Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get the length of sub children? 1

Status
Not open for further replies.

chandranam

Programmer
Jan 19, 2010
1
IN
Hi,

I am new to the XML. Learning from all tutorial site.

I want to count the number of sub-children with in a child. Also when i used the xmlDoc.documentElement.childNodes[1].childNodes.length, i got 9 instead of 4. How to count the sub-children length (of element book)?

Below is the xml code I am using.

<?xml version="1.0"?>
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
 
[0]>Also when i used the xmlDoc.documentElement.childNodes[1].childNodes.length, i got 9 instead of 4.
Either you mean you got 7 instead of 4 or you mean nothing.

[1] This shows you what is going on for mozilla-series of browser.
[tt]
var cnodes=oroot.childNodes[1].childNodes;
for (var i=0; i<cnodes.length;i++) {
alert('['+(i+1)+'] type:'+cnodes.nodeType+';'+
((cnodes.nodeType==3)?'nodeValue: '+escape(cnodes.nodeValue):'nodeName: '+cnodes.nodeName));
}
[/tt]
[2] nodeType 1 for element node; 3 for text node. Element node has nodeName well-defined, text node has nodeValue well defined.

[3] The parsing engine for the firefox etc has ignorable whitespace preserved by default. Some engine has the contrary as default such as ie (msxml2).
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top