LF Example parsing XML from a string not a file

B

Bob Sparks

Say this may seem odd but I can't find an example of how to parse XML
that is passed in a string and not a file. All the examples assume you
want to parse a file not a a chunk of memory already read in.

Does any one have an example on the web?

Bob
 
B

Bob Sparks

Thanks

I did this

docBuilder.parse( new InputSource( new StringReader( userClassTree.toString() ) ) );

Now I need to figure out how to get the parser to decode
"S & M toys" to
"S & M toys" when I getAttribute.

From reading the what Toni provided it looks like I need to setEncoding().

Bob
 
T

Toni Uusitalo

&whatever; constructs are entities (or < like things which are called
character references), they've got nothing to do with encoding. &amp; and 4
four other
predefined entities SHOULD have been decoded/expanded when your document
has been parsed i.e. you should get your "SAX & Markup toys" properly.
Check you parser's settings if there's something that preserves those
predefined entities.
(my Java xml parsing is a bit rusty at the moment, maybe somebody else could
help)

encoding: to put it short it's format in which you save/serialize your
document. When you load it you must have proper <? xml version="1.0"
encoding="whatever"?> set unless you're using (saved as) UTF-8 which is
default for parser if no encoding is specified.

http://www.ibiblio.org/xml/books/xmljava/ could give you answer on this too,
I recommend reading it, very useful book. reading it might be a bit on the S
&amp; M side for xml beginner in cos it's so detailed, but it's very good
book.

with respect,
Toni Uusitalo

Bob Sparks said:
Thanks

I did this

docBuilder.parse( new InputSource( new StringReader(
userClassTree.toString() ) ) );
 
G

GIMME

Something like this ...

Document doc = (new SAXBuilder()).build( new StringReader(xml));
 

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,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top