Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / April 2007

Tip: Looking for answers? Try searching our database.

Handling (retain) special characters when parsing XML?

Thread view: 
Piper707@hotmail.com - 05 Apr 2007 00:10 GMT
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
Greg R. Broderick - 05 Apr 2007 14:47 GMT
Piper707@hotmail.com wrote in news:1175728257.299882.68170
@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

Signature

---------------------------------------------------------------------
Greg R. Broderick            gregb+usenet200612@blackholio.dyndns.org

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



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.