Converting a Java String to XML with escape characters

T

thelemmings

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
 
D

Daniel Pitts

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

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[")
+ "]]>";
 
T

TechBookReport

Daniel said:
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

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[")
+ "]]>";
There's also URLEncode/URLDecode, which works on the basic
encoding/decoding of strings.

Pan
 
U

usenetuser

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

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.
 
O

opalpa opalpa

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

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
(e-mail address removed)
http://opalpa.info/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top