Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Converting a Java String to XML with escape characters

Reply
Thread Tools

Converting a Java String to XML with escape characters

 
 
thelemmings@gmail.com
Guest
Posts: n/a
 
      02-04-2007
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

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      02-04-2007
On Feb 4, 1:43 pm, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> 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[")
+ "]]>";


 
Reply With Quote
 
 
 
 
TechBookReport
Guest
Posts: n/a
 
      02-05-2007
Daniel Pitts wrote:
> On Feb 4, 1:43 pm, "(E-Mail Removed)" <(E-Mail Removed)>
> wrote:
>> 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

--
TechBookReport Java http://www.techbookreport.com/JavaIndex.html
 
Reply With Quote
 
Tim Slattery
Guest
Posts: n/a
 
      02-05-2007
"(E-Mail Removed)" <(E-Mail Removed)> wrote:

>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
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://members.cox.net/slatteryt
 
Reply With Quote
 
usenetuser@hotmail.co.uk
Guest
Posts: n/a
 
      02-05-2007
On 4 Feb, 21:43, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> 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.

 
Reply With Quote
 
opalpa opalpa@gmail.com http://opalpa.info
Guest
Posts: n/a
 
      02-05-2007
On Feb 4, 5:43 pm, "(E-Mail Removed)" <(E-Mail Removed)>
wrote:
> 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 Removed)
http://opalpa.info/

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a function to remove escape characters from a string ? Stef Mientki Python 11 12-28-2008 02:19 AM
How to read strings cantaining escape character from a file and useit as escape sequences? slomo Python 5 12-02-2007 11:39 AM
String and escape characters polilop Java 2 12-15-2006 03:18 PM
xml parsing escape characters Luis P. Mendes Python 16 01-22-2005 12:49 AM
converting html escape sequences to unicode characters harrelson Python 3 12-10-2004 08:15 AM



Advertisments