XML Parser

M

Michael

hello,
I'm quite new to XML parsing, so please bare with me. I'm learning
about Xerces, and I was wondering how people extract data after the
document being parsed. For example, I have:

<WEATHER>
<CITY NAME="New York">
<TEMP>64</TEMP>
</CITY>
</WEATHER>

and want to create an object for this as Weather {city, temp}.
After creating a DOMparser, then parse via the parse() method, then
should the node be checked if it's of "Weather", create an instance,
and then traverse for "City" and "Temp"? Is this the usual way of
extracting?

Any help would be appreciated.
Thank you very much,
Michael
 
J

Josef Garvi

Michael said:
<WEATHER>
<CITY NAME="New York">
<TEMP>64</TEMP>
</CITY>
</WEATHER>

and want to create an object for this as Weather {city, temp}.
After creating a DOMparser, then parse via the parse() method, then
should the node be checked if it's of "Weather", create an instance,
and then traverse for "City" and "Temp"? Is this the usual way of
extracting?

I think it's just a matter of finding what seem most effective for you.
Personally, I would create a constructor:

public Weather(Element weatherNode) {
// walk through subelements here to initialise fields
}

and let the Weather object initialise itself based on the node. (possibly
throwing an exception if the subsequent xml nodes are invalid)

Then all I would need would be to loop through all weather elements and
call my constructor for them.
If my xml document was complex, and the weather nodes were buried deep
down, I could use XPath to immediately resolve to the weather element
collection.

--
Josef Garvi

"Reversing desertification through drought tolerant trees"
http://www.eden-foundation.org/

new income - better environment - more food - less poverty
 

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,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top