Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2007

Tip: Looking for answers? Try searching our database.

Converting a Java String to XML with escape characters

Thread view: 
thelemmings@gmail.com - 04 Feb 2007 21:43 GMT
Hi,

I would like to convert a Java String to an XML formatted string with
escape characters. For example turning "&" to "&amp;", "<" or "&lt;",
and so on.

Is it possible to achieve this without resorting to external
libraries? (I'm using Java 5)

Thanks,
Luc
Daniel Pitts - 04 Feb 2007 22:41 GMT
On Feb 4, 1:43 pm, "thelemmi...@gmail.com" <thelemmi...@gmail.com>
wrote:
> Hi,
>
[quoted text clipped - 7 lines]
> Thanks,
> Luc

You'd have you write the code yourself, there isn't anything built
in.  If you writting a JSP, the JSTL provides methods to escape, but
I'm assuming you're talkign about using the Java 5 Standard Edition

I think the easiest way (although not %100 perfect)

myString = myString.replaceAll("&", "&amp;");
myString = myString.replaceAll("<", "&lt;");

Alternatively, you could use CDATA escaping:
myString = "<![CDATA[" + myString.replaceAll("]]>", "]]>]]><![CDATA[")
+ "]]>";
TechBookReport - 05 Feb 2007 10:19 GMT
> On Feb 4, 1:43 pm, "thelemmi...@gmail.com" <thelemmi...@gmail.com>
> wrote:
[quoted text clipped - 22 lines]
> myString = "<![CDATA[" + myString.replaceAll("]]>", "]]>]]><![CDATA[")
> + "]]>";

There's also URLEncode/URLDecode, which works on the basic
encoding/decoding of strings.

Pan

Signature

TechBookReport Java http://www.techbookreport.com/JavaIndex.html

Tim Slattery - 05 Feb 2007 15:24 GMT
>Hi,
>
> I would like to convert a Java String to an XML formatted string with
>escape characters. For example turning "&" to "&amp;", "<" or "&lt;",
>and so on.

Roedy Green's Entity Strip/Insert utility is a *very* complete
solution for this problem. Look here:
http://mindprod.com/products1.html#ENTITIES

--  
Tim Slattery
Slattery_T@bls.gov
http://members.cox.net/slatteryt
usenetuser@hotmail.co.uk - 05 Feb 2007 15:59 GMT
On 4 Feb, 21:43, "thelemmi...@gmail.com" <thelemmi...@gmail.com>
wrote:
> Hi,
>
[quoted text clipped - 7 lines]
> Thanks,
> Luc

If you are wanting to do this so as it put the String into an XML --
why don't you just use a CDATA section in the XML removing the need to
escape the data.
opalpa opalpa@gmail.com http://opalpa.info - 05 Feb 2007 16:19 GMT
On Feb 4, 5:43 pm, "thelemmi...@gmail.com" <thelemmi...@gmail.com>
wrote:
> Hi,
>
[quoted text clipped - 7 lines]
> Thanks,
> Luc

How about using javax.xml.transform.Transformer ?

The transformer does not work on String directly.  I insert my Strings
into a document and use Transformer to get my formatted string.

Cheers.
opalpa
opalpa@gmail.com
http://opalpa.info/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.