Parsing an XML Document the Easy Way

By David Wall, ITworld |  How-to Add a new comment

Once you've read an XML document into memory, like this...

var xmlDocHolder = new ActiveXObject("MSXML2.DOMDocument")
xmlDocHolder.load("http://www.davidwall.com\countryList.xml")

...you can begin to read elements of that document. The document
countrylist.xml contains, in part, this structure:



United States of America


Japan

To refer to data, we refer to a nested series of arrays. The element
containing the data "Japan" would be referred to like this:

JapanNode = xmlDocHolder.childNodes[0].childNodes[1].childNodes[0]

That is, we want to refer to the first (0) element (officialName) of
the second (1) element (country) of the first (0) element
(countryList). The word "Japan" itself -- the data -- would be
referenced as:

JapanText = xmlDocHolder.childNodes[0].childNodes[1].childNodes[0].text

    Add a comment

    Post a comment using one of these accounts
    Or join now
    At least 6 characters

    Note: Comment will appear soon after you have activated your account.
    Obscene/spam comments will be removed and accounts suspended.
    The information you submit is subject to our Privacy Policy and Terms of Service.

    ITworld LIVE

    Ask a question

    Ask a Question