2

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.

5
  • If it were happening to me, I'd want to check the "nodeType" attribute first. Perhaps there are CDATA (text) nodes mixed in? Commented Jan 16, 2011 at 22:12
  • Oh also, this isn't really about parsing the XML, since if you're traversing the DOM something has already parsed it (the browser, presumably). Commented Jan 16, 2011 at 22:12
  • totally agree with @Pointy.. using a JS xml parser instead? (i.e. sarissa... dev.abiss.gr/sarissa) Commented Jan 16, 2011 at 22:14
  • Hmm... I did check the nodeType for each child, and for some strange reason it varies between 1 and 3. Strange because I only have tags inside the root node, thus it should only have element nodes and not text nodes. Commented Jan 16, 2011 at 22:32
  • The XML spec requires whitespace 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. Commented Jan 17, 2011 at 0:48

3 Answers 3

3

This is actually a clarification of Hemlock's answer. I'm putting it here instead of commenting his answer because I don't have space to draw pretty ASCII art in comments.

Lets say we have the following XML:

<a><b></b><c></c></a>

This generates the following DOM:

<a>--.
     |
    <b>
     |
    <c>

which is generally what you'd expect.

Lets say we now have the following XML:

<a>
   <b></b>
   <c></c>
</a>

You would think this generates the same DOM. But according to the standard, you'd be wrong. Instead the standard requires it to generate the following DOM:

<a>--.
     |
    "\n   "
     |
    <b>
     |
    "\n   "
     |
    <c>
     |
    "\n"

Yes, the spec says all those whitespace should be captured in the DOM. Almost all XML implementations out there does this (not only in browsers). The only exception being IE (and by extension the XML engine in JScript) because Microsoft didn't care much about violating standards.

Personally this is useless 99.999% of the time. About the only time this would be useful is if you're trying to implement an XML code editor. But it's in the standards so browsers need to implement it if they want to be standards compliant.

Sign up to request clarification or add additional context in comments.

1 Comment

I have mixed feelings about the outcome of this.
2

You are getting text nodes (nodeType == 3) mixed in with elements. The text nodes probably only contain white space. You just want to filter your loop on nodeType (like Pointy said).

var root = xmlData.getElementsByTagName("library_geometries")[0]; 

for (i = 0; i < root.childNodes.length; i++) //get all the geometries
{  
 geom =  root.childNodes[i];  
 if (geom.nodeType == 1) {
  alert(geom.tagName);
 }
}

https://developer.mozilla.org/en/nodeType

Comments

0

Wait the html document to be parsed. You should run this piece of script when the document is ready, in onload() the document is not parsed yet so it`s possible you wont find tags.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.