XML Parser

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael

    #1

    XML Parser

    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

    #2
    Re: XML Parser

    Michael wrote:
    [color=blue]
    > <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?[/color]

    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"


    new income - better environment - more food - less poverty

    Comment

    Working...