Getting only direct children in XML

A

Andrew May

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
 
M

Martin Honnen

Andrew said:
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.
 
R

RobG

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
 
A

Andrew May

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
 
M

Martin Honnen

Andrew said:
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.
 
P

pr

Martin said:
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!
 
D

David Golightly

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
 
A

Andrew May

David said:
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
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top