Connecting Tech Pros Worldwide Forums | Help | Site Map

childNodes.length problem

Q1tum
Guest
 
Posts: n/a
#1: Jan 4 '06
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


RobG
Guest
 
Posts: n/a
#2: Jan 4 '06

re: childNodes.length problem


Q1tum wrote:[color=blue]
> 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[/color]

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.

[color=blue]
>
> Is this wrong[/color]

No.

[color=blue]
> or maybe there is a better / other way of counting the
> child amount?[/color]

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
Martin Honnen
Guest
 
Posts: n/a
#3: Jan 4 '06

re: childNodes.length problem




Q1tum wrote:

[color=blue]
> <?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?[/color]

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/
Q1tum
Guest
 
Posts: n/a
#4: Jan 4 '06

re: childNodes.length problem


Thank you,

your script works fine!

Martin Honnen thanks as well for explaining

Closed Thread