Getting only direct children in XML

Discussion in 'Javascript' started by Andrew May, Sep 21, 2007.

  1. Andrew May

    Andrew May Guest

    This is only my first AJAX based application so this may be a trivial
    question but I cannot find any way of getting at the data I want.

    So, I have an XML file of the form:

    <data>
    <dataelement> // one or more instances of this
     
    Andrew May, Sep 21, 2007
    #1
    1. Advertisements

  2. With the core DOM you need to write a helper function that loops through
    the childNodes and looks for the element nodes with the name you are
    after (e.g. status).
    Most browsers by now also support XPath so with Mozilla, Opera 9 you can
    use the W3C DOM Level 3 XPath API to use an XPath expression of e.g.
    status
    relative to your data element.
    With IE 6 and later there is also XPath support in the form of
    selectSingleNode/selectNodes exposed on DOM nodes.
     
    Martin Honnen, Sep 21, 2007
    #2
    1. Advertisements

  3. Andrew May

    RobG Guest

    You could loop through the childNodes collection looking for nodes
    with a nodeName or tagName of "status", or use XPath, which should be
    very much faster for large documents. It's not supported by IE, but
    if you're working with XML documents you may not care about that.

    Try this post:

    <URL:
    http://groups.google.com.au/group/c...t&q=document.evaluate&rnum=1#989d2ae90dc99f1b
     
    RobG, Sep 21, 2007
    #3
  4. Andrew May

    Andrew May Guest

    Thanks to both of you. Looping through childNodes it is then. IE support
    being a requirement and it not being a particularly big piece of XML.
    Being new to this I wondered whether there might be some magic
    incantation that I had missed but no real problem writing a bit of code.

    Thanks again.

    Andrew
     
    Andrew May, Sep 21, 2007
    #4
  5. IE 6 and later come with MSXML 3 for XML parsing and XML DOM and MSXML 3
    supports XPath node selection using selectSingleNode/selectNodes, once
    you do xmlDocumentInstance.setProperty('SelectionLanguage', 'XPath').
    So an API exists with IE only it is different from the one you use with
    Mozilla or Opera 9.
     
    Martin Honnen, Sep 21, 2007
    #5
  6. Andrew May

    pr Guest

    I second this approach

    var result = xml.selectNodes("/data/status");

    easy!
     
    pr, Sep 21, 2007
    #6
  7. This is actually a DOM question, which is an interface that's
    implemented in lots of languages, so it's not strictly JS. Also,
    XMLHttpRequest is unfortunately named - XML is quite heavyweight for
    most Ajax applications; JSON is much easier to work with in
    JavaScript, and takes much less code to traverse. Plus, it's easy for
    server-side languages to parse; there are lots of free parsing
    libraries out there for your language of choice, if you have the
    misfortune to not be working in Python on the server ;) . If you
    haven't yet, I'd recommend taking a look at JSON.

    But to answer your question, you need node.childNodes. Be aware that,
    as you iterate through this NodeList, you'll have to check the
    NodeType property to ensure that it equals 1 (node.ELEMENT_NODE),
    because childNodes also may include text nodes, comment nodes, CDATA
    nodes, etc., none of which have a tagName property. (Alternatively,
    you may traverse the childNodes as a linked list, starting with
    node.firstChild, then assigning successively node = node.nextSibling
    until you reach node.lastChild). Let me know if you need code samples
    here. Good luck!

    -David
     
    David Golightly, Sep 21, 2007
    #7
  8. Andrew May

    Andrew May Guest

    I did wonder about that - whether the question might be better in an XML
    group. But, having said that I have had some useful answers from the
    folks here.
    That looks interesting. Don't think I've come across JSON before. I'll
    take a look. What I am trying to achieve is not complex so JSON looks
    ideal. I've plenty of programming experience but am new to all this
    PHP/web/javascript stuff.

    Thanks,

    Andrew
     
    Andrew May, Sep 25, 2007
    #8
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.