 |
AppletTalk.com Java discussions newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Darryl R. Guest
|
Posted: Thu Aug 07, 2003 5:50 pm Post subject: Looking for source code to XML-encode a string |
|
|
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.
|
|
| Back to top |
|
 |
La'ie Techie Guest
|
Posted: Wed Oct 15, 2003 6:07 am Post subject: Re: Looking for source code to XML-encode a string |
|
|
On Thu, 07 Aug 2003 17:50:20 +0000, Darryl R. wrote:
| Quote: | 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("&");
else if ( c == '<' )
work.append("<");
else if ( c == '>' )
work.append(">");
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
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|