AppletTalk.com Forum Index AppletTalk.com
Java discussions newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Looking for source code to XML-encode a string

 
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.misc
View previous topic :: View next topic  
Author Message
Darryl R.
Guest





PostPosted: Thu Aug 07, 2003 5:50 pm    Post subject: Looking for source code to XML-encode a string Reply with quote



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





PostPosted: Wed Oct 15, 2003 6:07 am    Post subject: Re: Looking for source code to XML-encode a string Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    AppletTalk.com Forum Index -> comp.lang.java.misc All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.