XML DOM childNodesProperty
Element object
Definition and Usage
The childNodes property returns a NodeList containing the child nodes of the selected node.
If the selected node has no child nodes, this property returns a NodeList containing no nodes.
Syntax
Tips and notes
Tip:When looping through the childNodes list, using the nextSibling property is more efficient than using the parent object's childNodes list.
Example 1
The following code snippet usesloadXMLDoc()put "books.xmlLoad into xmlDoc, and get the text node from the first <title> element in "books.xml":
Example
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
document.write(x.nodeValue);
The code above will output:
Try it Yourself »
Example 2
The following code snippet usesloadXMLDoc()put "books.xmlLoad into xmlDoc and get the number of child nodes from the first <title> element in "books.xml":
Example
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
document.write(x.length);
In Internet Explorer, the above code will output:
In Mozilla browsers, the above code will output:
Try it Yourself »
Firefox and most other browsers treat empty whitespace or newlines generated between nodes as text nodes, while Internet Explorer ignores whitespace text nodes generated between nodes. Therefore, in the example above, the output differs.
To learn more about browser differences, please visit our XML DOM tutorial to access ourDOM browserChapter.
Element objectother extensions