Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Looking for source code to XML-encode a string

Reply
Thread Tools

Looking for source code to XML-encode a string

 
 
Darryl R.
Guest
Posts: n/a
 
      08-07-2003
Everyone, I'm trying to create a JSP page that returns an XML string to the
caller (browser). Some of the elements will contain characters that should
be encoded before being added to the XML string (e.g. A&P should be changed
to A&P before it is added to the string). I'm looking for some code that
will do this, since the org.w3c.dom package doesn't seem to have any classes
with static methods that do this.

What I'm looking for is a the source code to create a method that accepts an
unencoded string and returns an encoded string. For example:

String s = "A&P";
String encodedS = encode(s);
//The method should return "A&P"

Any help would be greatly appreciated.

Thanks,
Darryl R.


 
Reply With Quote
 
 
 
 
La'ie Techie
Guest
Posts: n/a
 
      10-15-2003
On Thu, 07 Aug 2003 17:50:20 +0000, Darryl R. wrote:

> What I'm looking for is a the source code to create a method that accepts
> an unencoded string and returns an encoded string. For example:
>
> String s = "A&P";
> String encodedS = encode(s);
> //The method should return "A&P"


public String encodeXML(String text)
{
int length = text.length;
StringBuffer work = new StringBuffer(length);
for ( int i=0; i < length; i++)
{
char c = text.charAt(i);
if ( c == '&')
work.append("&amp;");
else if ( c == '<' )
work.append("&lt;");
else if ( c == '>' )
work.append("&gt;");
else if ( c > 127 || Character.isISOControl(c) )
work.append("&#" + (int) c);
else
work.append(c);
}
return work.toString();
}

You could also use regular expressions, if you are using J2SE 1.4 . You
may want to encode the quotes.

Aloha,
La'ie Techie

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Panel with vivible=false in source code / large source Jens-Oliver Murer ASP .Net 1 10-17-2008 01:00 PM
Data Recovery SOURCE CODE ( SOURCE CODES of Professional Data Recovery Software ) Author Tarun Tyagi C Programming 0 12-29-2004 05:10 PM
Data Recovery SOURCE CODE ( SOURCE CODES of Professional Data Recovery Software ) Author Tarun Tyagi Cisco 0 12-29-2004 05:03 PM
Looking for simple rectangle drawing source code Ramon Java 2 12-18-2004 10:06 AM
can't use View, Source menu to open the source code. Jenny Javascript 3 12-17-2004 07:01 AM



Advertisments