I am trying to extract values from the xml document and print them. I also want to count the number of children(child nodes) each node has.That is the first tag has 2 child
Element.childNodes method returns all types of nodes, including whitespace textnodes. It may not be what you want. If you only care for the number of child elements, use childElementCount.
var b = xmlDoc.getElementsByTagName("B")[0];
alert(b.childElementCount); //should output 2
I haven't tried in IE, it may not work.
Else, if you want a the element list, use childrenchildren not supported on non HTML doc. You can try this function:
function getChildren(element) {
var nodes = element.childNodes;
var children = [];
for (var i = 0; i < nodes.length; i++) {
if (nodes[i].nodeType == Node.ELEMENT_NODE) children.push(nodes[i]);
}
return children;
}
getChildren(b).length;