By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,508 Members | 1,856 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,508 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Jun 16 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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
Jun 16 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.