Hi,
I would like to convert a Java String to an XML formatted string with
escape characters. For example turning "&" to "&", "<" or "<",
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("&", "&");
myString = myString.replaceAll("<", "<");
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 "&", "<" or "<",
>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/