XML Parser

Michael (18)

Usenet Archive
Joined
Sep 15, 2003
Messages
13
Reaction score
0
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
 

Josef Garvi

Usenet Archive
Joined
Jul 23, 2003
Messages
68
Reaction score
0
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.
 

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
548,042
Messages
3,104,224
Members
248,132
Latest member
henrywalsh

Latest Threads

Top