Parsing XML in Java

J

Jonathan

Hi folks,

Here's the problem I'm having. I'm using a servlet to post an XML
document to a server, in turn that server will return a string
representation of an XML document. I'm using Xerces as my parser but
it seems to me anyways that Xerces can't parse a string representation
of the document only an object representing the file. I don't want to
write the information out to a file and then read it back in, so I was
wondering if anyone knew of a better way to do this.

Thanks for your help

Jonathan
 
R

rajesh.shiggaon

Hi Jonathan:

You can use the JAXP api's. Using JAXP api's through which you can
configure to use the Xerces as your parser. I am sure using JAXP apis
you can use easily convert the string xml variable to a input stream
and using that we can conert a sax/dom object .

Thanks
Rajesh S.
 
S

Steve W. Jackson

Hi folks,

Here's the problem I'm having. I'm using a servlet to post an XML
document to a server, in turn that server will return a string
representation of an XML document. I'm using Xerces as my parser but
it seems to me anyways that Xerces can't parse a string representation
of the document only an object representing the file. I don't want to
write the information out to a file and then read it back in, so I was
wondering if anyone knew of a better way to do this.

Thanks for your help

Jonathan

In the earlier versions of my app, I used Xerces exclusively and a
DOMParser in particular (from the Xerces2 implementation, not the newer
APIs). To handle XML placed onto the clipboard, I had to call its parse
method with an org.xml.sax.InputSource. An InputSource can be created
with its constructor that accepts an InputStream. And there are several
subclasses of InputStream, including a ByteArrayInputStream created from
a byte array -- which can be created from a String.

As another reply has indicated, you can use the standard JAXP calls to
configure Xerces as your parser. I now get my XML data directly from
the clipboard as a DataFlavor with an appropriate MIME type, converted
into an InputStream. But you can still make an InputStream from a
String just as I described above.

= Steve =
 
B

bugbear

Jonathan said:
Hi folks,

Here's the problem I'm having. I'm using a servlet to post an XML
document to a server, in turn that server will return a string
representation of an XML document. I'm using Xerces as my parser but
it seems to me anyways that Xerces can't parse a string representation
of the document only an object representing the file. I don't want to
write the information out to a file and then read it back in, so I was
wondering if anyone knew of a better way to do this.

Lets see.
A DocumentBuilder can parse:
File
InputSource
InputStream
(URI)

File sounds a bit unlikely, and making a stream
from a String involves makeing encoding assumptions.
Lets look at InputSource.

We can make an InputSource from:
InputStream
Reader

Reader sounds more than promising.
Looking for subclasses, we find StringReader...

So:

db.parse(new InputSource(new StringReader(myString)));

is the call you're looking for.

BugBear
 

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

Forum statistics

Threads
473,785
Messages
2,569,624
Members
45,318
Latest member
LuisWestma

Latest Threads

Top