By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,963 Members | 1,902 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,963 IT Pros & Developers. It's quick & easy.

childNodes.length problem

P: n/a
Hi all,

I have a problem with getting the amount of childs in a XML structure,
the strucure is somewhat like the following:

<?xml version="1.0" encoding="iso-8859-1"?>
<cms>
<num>21</num>
<xmlnames>
<field>id</field>
<field>name</field>
<field>availablefrom</field>
<field>availableuntill</field>
<field>owner</field>
</xmlnames>
</cms>

If I use the following to get te amount of childs for xmlnames I get an
amount of 11:
xmlObj.responseXML.getElementsByTagName('xmlnames' )[0].childNodes.length

Is this wrong or maybe there is a better / other way of counting the
child amount?

Regards,

Arno

Jan 4 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Q1tum wrote:
Hi all,

I have a problem with getting the amount of childs in a XML structure,
the strucure is somewhat like the following:

<?xml version="1.0" encoding="iso-8859-1"?>
<cms>
<num>21</num>
<xmlnames>
<field>id</field>
<field>name</field>
<field>availablefrom</field>
<field>availableuntill</field>
<field>owner</field>
</xmlnames>
</cms>

If I use the following to get te amount of childs for xmlnames I get an
amount of 11:
xmlObj.responseXML.getElementsByTagName('xmlnames' )[0].childNodes.length
Which seems correct to me. Empty text nodes will be inserted at the
end of each line where there is a new line or line feed character, so
there are empty text nodes after the opening 'xmlnames' tag and the 5
closing 'field' tags that are all children of the xmlnames element.

That's 6 empty text nodes plus the 5 'field' child nodes, making 11.


Is this wrong
No.

or maybe there is a better / other way of counting the
child amount?


If you want to get just the children with a tagName of 'field', then
use getElementsByTagName(). Note that will get all the descendants of
xmlnames that have a tag called 'field', not just the direct children.

If you want explicitly the children, then use a loop to get them:

var t = xmlObj.responseXML.getElementsByTagName('xmlnames' );
var c = t[0].childNodes;
var k = [];
for (var i=0, len=c.length; i<len; ++i){
if('field' == c[i].tagName){
k[k.length] = c[i];
}
}

// k is now an array of the child nodes of xmlnames that are
// field nodes.

--
Rob
Jan 4 '06 #2

P: n/a


Q1tum wrote:

<?xml version="1.0" encoding="iso-8859-1"?>
<cms>
<num>21</num>
<xmlnames>
<field>id</field>
<field>name</field>
<field>availablefrom</field>
<field>availableuntill</field>
<field>owner</field>
</xmlnames>
</cms>

If I use the following to get te amount of childs for xmlnames I get an
amount of 11:
xmlObj.responseXML.getElementsByTagName('xmlnames' )[0].childNodes.length

Is this wrong or maybe there is a better / other way of counting the
child amount?


childNodes contains all kind of nodes and in your case there a text
nodes with white space between the element nodes e.g. you have

<xmlnames>^^^^^white space text node here
^^^^^ <field>id</field>^^^^ and here
^^^^^ <field>name</field>^^^^ and here ^^^^
</xmlnames>

and not

<xmlnames><field>id</field><field>name</field></xmlnames>

If you are looking only for element nodes then depending on what you
need and depending on the structure of the XML you need to loop through
childNodes and check for nodeType == 1 or you can use
getElementsByTagName('*') or in your case simply

xmlObj.responseXML.getElementsByTagName('xmlnames' )[0].getElementsByTagName('field')

Be aware that getElementsByTagName finds all descendant elements so with
nested structures it is not equivalent to childNodes and checking
nodeType == 1.

But in many cases including your example file when people run into white
space text nodes issues with childNodes then simply switching to
getElementsByTagName to find the elements needed works without problems.

Besides that there is also XPath and NodeFilter/TreeWalker APIs but
support for that is not as wide spread as childNodes/nodeType and
getElementsByTagName and XPath for instance is supported in MSXML 3 and
later for IE/Win and in Mozilla but the APIs are very different.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jan 4 '06 #3

P: n/a
Thank you,

your script works fine!

Martin Honnen thanks as well for explaining

Jan 4 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.