Is there a way to loop through the immediate children of an XML node in JavaScript, without using jquery or a similar library? I tried to use ".childNodes", but for some reason it doesn't work as it should. ".childNodes.length" return a number which is usually greater than the number of immediate nodes, and all of the tag names (using .tagName) are for some reason undefined. I know that my XML data is formated correctly because if I call ".getElementsByTagName()" using the tags of the immediate children, it works as it should. Some examples of my dilemma :
var root = xmlData.getElementsByTagName("library_geometries")[0];
for (i = 0; i < root.childNodes.length; i++) //get all the geometries
{
geom = root.childNodes[i];
alert(geom.tagName);
}
------------------------------------------------------
geom = root.getElementsByTagName("geometry");
for (i = 0; i < geom.length; i++) //get all the geometries
{
alert(geom[i].tagName);
}
First one doesn't work at all, second one works in this example.
requireswhitespace to be preserved as text nodes. These include tabs, newlines and spaces. You are probably seeing this issue because your XML is formatted for human consumption. If you remove all unnecessary whitespace then all those type3 nodes will disappear. Interestingly, IE is the only browser which does not have this problem because it doesn't care about violating spec. Personally I think the spec is stupid.