Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > need Java function to convert XML special chars < > to &lt; &gt;

Reply
Thread Tools

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

 
 
Robert Dodier
Guest
Posts: n/a
 
      06-16-2006
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

 
Reply With Quote
 
 
 
 
Joe Kesselman
Guest
Posts: n/a
 
      06-16-2006
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
 
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
How to truncate char string fromt beginning and replace chars instring by other chars in C or C++? Hongyu C++ 9 08-08-2008 12:18 PM
XML Javascript Special Chars encoding design4future Perl Misc 0 02-20-2006 01:52 PM
Floats to chars and chars to floats Kosio C Programming 44 09-23-2005 09:49 AM
receiving ??? chars instead of "special" chars M.Posseth ASP .Net Web Services 3 11-16-2004 07:00 PM
Special chars (זרו): aspx.vb vs. js hansiman ASP .Net 4 09-24-2004 06:44 AM



Advertisments