Handling (retain) special characters when parsing XML?

P

Piper707

Hi,

We need help with processing special characters when processing XML
consecutively first by SAX and then converting that output into DOM.

This is what we do:

The input XML has all special chacters like ampersand replaced with
the correct strings: &

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse( new File( FileWithXml ), handler );

the handler saves all the parsed XML into a string in a particular
format. - in the parsed XML, the & gets converted into &

String parsedString = parsedXml.toString();

parsedString needs to be converted into a document:

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
document = factory.newDocumentBuilder().parse(new InputSource(new
StringReader(parsedString)));

But due to the presence of &, we cannot convert to a document unless &
is again replaced with &

Is there a way to retain special characters the first time around, so
we dont have to replace all occurences again before converting to a
document? Can a custom entity reference handler be used for anything
like this?

Thanks for any help
Rohit
 
G

Greg R. Broderick

(e-mail address removed) wrote in @p77g2000hsh.googlegroups.com:
Is there a way to retain special characters the first time around, so
we dont have to replace all occurences again before converting to a
document? Can a custom entity reference handler be used for anything
like this?

I'd recommend not attempting to re-use the parsed XML (parsed by the SAX
parser) as input to the DOM parser. Instead, just create a new InputSource
from the input file and use that to feed the DOM parser.

Cheers
GRB

--
---------------------------------------------------------------------
Greg R. Broderick (e-mail address removed)

A. Top posters.
Q. What is the most annoying thing on Usenet?
---------------------------------------------------------------------
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,562
Members
45,038
Latest member
OrderProperKetocapsules

Latest Threads

Top