need Java function to convert XML special chars < > to &lt; &gt;

Discussion in 'XML' started by Robert Dodier, Jun 16, 2006.

  1. Hello,

    Sorry for asking what must be a FAQ, but I wasn't able to find the
    answer.

    I have an XML document fragment which I want to store as a text string.
    I want a function to convert any XML special characters such as < >
    into
    the corresponding character entities. I'm working with Java.

    Thanks for any light you can shed on this problem.

    Robert Dodier
     
    Robert Dodier, Jun 16, 2006
    #1
    1. Advertising

  2. Robert Dodier wrote:
    > I have an XML document fragment which I want to store as a text string.
    > I want a function to convert any XML special characters such as < >
    > into
    > the corresponding character entities.


    If you're using any of the standard XML tools (SAX, DOM, XSLT, ...) you
    really shouldn't have to deal with any of that. They've already
    implemented XML serializers which will do the right thing, and their
    Java APIs let you just enter the text directly. In other words, if you
    put a & character into a SAX characters() call, or into a DOM text node,
    the XML serializer will automagically deal with converting it to &amp;
    when you write that XML out.

    If you really want a function... well, the easiest solution is to borrow
    one from one of the open-source packages which already handles it, such
    as the serializers in Xerces or Xalan. Note that it's a bit uglier than
    it looks, since you may also have to deal with encoding issues unless
    you're keeping everything in UTF-8 or UTF-16.

    --
    () ASCII Ribbon Campaign | Joe Kesselman
    /\ Stamp out HTML e-mail! | System architexture and kinetic poetry
     
    Joe Kesselman, Jun 16, 2006
    #2
    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. hansiman
    Replies:
    4
    Views:
    838
    hansiman
    Sep 24, 2004
  2. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,309
    Tim Rentsch
    Sep 23, 2005
  3. Hongyu
    Replies:
    9
    Views:
    930
    James Kanze
    Aug 8, 2008
  4. M.Posseth

    receiving ??? chars instead of "special" chars

    M.Posseth, Nov 15, 2004, in forum: ASP .Net Web Services
    Replies:
    3
    Views:
    251
    Dan Rogers
    Nov 16, 2004
  5. design4future

    XML Javascript Special Chars encoding

    design4future, Feb 20, 2006, in forum: Perl Misc
    Replies:
    0
    Views:
    122
    design4future
    Feb 20, 2006
Loading...

Share This Page