childNodes.length problem

Q

Q1tum

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
 
L

Lee

Q1tum said:
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?

I count 11 child nodes in your examples.
Five of them are element nodes.
The other six are text nodes containing whitespace.
 
R

RobG

Q1tum said:
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.tagName){
k[k.length] = c;
}
}

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

Martin Honnen

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.
 

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,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top