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. Advertising

  2. Andrew May wrote:

    > Using getElementsByTagName on <data> gets me an array containing all
    > <status> elements including those of the child <dataelements>s. What I
    > need is some means to extract from <data> only those <status> elements
    > that are directly children of the <data> and not those that are children
    > of the child <dataelement>s. I could probably use LastChild to get what
    > I want but cannot guarantee that the <status> will necessarily come
    > after the <dataelement>s.


    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
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 21, 2007
    #2
    1. Advertising

  3. Andrew May

    RobG Guest

    On Sep 21, 9:17 pm, Andrew May <> wrote:
    > 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
    > .
    > some data
    > .
    > <status>
    > .
    > some status information
    > .
    > </status>
    > </dataelement>
    > <status>
    > .
    > some status information
    > .
    > </status>
    > </data>
    >
    > The significant feature is that both the enclosing <data> and each
    > <dataelement> have <status> elements that are identical in format so I
    > want to keep the <status> tag.
    >
    > I can use getElementsByTagName to extract all the <dataelements> and
    > then use getElementByTagName to get all <status> elements in each
    > <dataelement>. There should only be one so I can use FirstChild to
    > access it.
    >
    > Using getElementsByTagName on <data> gets me an array containing all
    > <status> elements including those of the child <dataelements>s. What I
    > need is some means to extract from <data> only those <status> elements
    > that are directly children of the <data> and not those that are children
    > of the child <dataelement>s. I could probably use LastChild to get what
    > I want but cannot guarantee that the <status> will necessarily come
    > after the <dataelement>s.


    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
    >



    --
    Rob
     
    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. Andrew May wrote:
    > 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.


    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
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Sep 21, 2007
    #5
  6. Andrew May

    pr Guest

    Martin Honnen wrote:

    > 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.


    I second this approach

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

    easy!
     
    pr, Sep 21, 2007
    #6
  7. On Sep 21, 4:17 am, Andrew May <> wrote:
    > 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
    > .
    > some data
    > .
    > <status>
    > .
    > some status information
    > .
    > </status>
    > </dataelement>
    > <status>
    > .
    > some status information
    > .
    > </status>
    > </data>
    >
    > The significant feature is that both the enclosing <data> and each
    > <dataelement> have <status> elements that are identical in format so I
    > want to keep the <status> tag.
    >
    > I can use getElementsByTagName to extract all the <dataelements> and
    > then use getElementByTagName to get all <status> elements in each
    > <dataelement>. There should only be one so I can use FirstChild to
    > access it.
    >
    > Using getElementsByTagName on <data> gets me an array containing all
    > <status> elements including those of the child <dataelements>s. What I
    > need is some means to extract from <data> only those <status> elements
    > that are directly children of the <data> and not those that are children
    > of the child <dataelement>s. I could probably use LastChild to get what
    > I want but cannot guarantee that the <status> will necessarily come
    > after the <dataelement>s.
    >
    > I hope this is clear.
    >
    > Thanks,
    >
    > Andrew


    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

    David Golightly wrote:

    > This is actually a DOM question, which is an interface that's
    > implemented in lots of languages, so it's not strictly JS.


    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.

    > 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.


    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. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Esteban Valencia
    Replies:
    0
    Views:
    375
    Luis Esteban Valencia
    Jun 20, 2005
  2. Eric
    Replies:
    12
    Views:
    866
    Roedy Green
    Aug 31, 2005
  3. Clint
    Replies:
    2
    Views:
    333
    Clint
    May 5, 2006
  4. asp:menu only show children

    , Nov 21, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    150
  5. Luke Dalessandro

    element.children has no properties (mozilla only)

    Luke Dalessandro, Jun 22, 2005, in forum: Javascript
    Replies:
    6
    Views:
    119
Loading...

Share This Page