Converting a Java String to XML with escape characters

Discussion in 'Java' started by thelemmings@gmail.com, Feb 4, 2007.

  1. Guest

    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
    , Feb 4, 2007
    #1
    1. Advertising

  2. Daniel Pitts Guest

    On Feb 4, 1:43 pm, "" <>
    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[")
    + "]]>";
    Daniel Pitts, Feb 4, 2007
    #2
    1. Advertising

  3. Daniel Pitts wrote:
    > On Feb 4, 1:43 pm, "" <>
    > 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
    TechBookReport, Feb 5, 2007
    #3
  4. Tim Slattery Guest

    Tim Slattery, Feb 5, 2007
    #4
  5. Guest

    On 4 Feb, 21:43, "" <>
    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.
    , Feb 5, 2007
    #5
  6. On Feb 4, 5:43 pm, "" <>
    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

    http://opalpa.info/
    opalpa http://opalpa.info, Feb 5, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. harrelson
    Replies:
    3
    Views:
    3,893
    Craig Ringer
    Dec 10, 2004
  2. Luis P. Mendes

    xml parsing escape characters

    Luis P. Mendes, Jan 19, 2005, in forum: Python
    Replies:
    16
    Views:
    1,426
    =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?=
    Jan 22, 2005
  3. polilop

    String and escape characters

    polilop, Dec 15, 2006, in forum: Java
    Replies:
    2
    Views:
    615
    polilop
    Dec 15, 2006
  4. slomo
    Replies:
    5
    Views:
    1,522
    Duncan Booth
    Dec 2, 2007
  5. Stef Mientki
    Replies:
    11
    Views:
    10,542
    Martin
    Dec 28, 2008
Loading...

Share This Page