XML DOM childNodes Property
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
elementNode.childNodes
Tips and Notes
**Tip:** To loop through a childNodes list, using the nextSibling property is more efficient than using the parent object's childNodes list.
Example 1
The following code snippet uses loadXMLDoc() to load "books.xml" Load into xmlDoc, and get the text node from the first
Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("title")[0].childNodes[0];
document.write(x.nodeValue);
The code above will output:
Everyday Italian
Try it »
Example 2
The following code snippet uses loadXMLDoc() to load "books.xml" Load into xmlDoc, and get the text node from the first
Example
xmlDoc=loadXMLDoc("books.xml");
x=xmlDoc.getElementsByTagName("book")[0].childNodes;
document.write(x.length);
In Internet Explorer, the code above will output:
4
In Mozilla browsers, the code above will output:
9
Try it »
Firefox and most other browsers treat the empty spaces or line breaks generated between nodes as text nodes, while Internet Explorer ignores the whitespace text nodes generated between nodes. Therefore, in the example above, the output differs.
To learn more about browser differences, please visit our DOM Browsers section.
Element Object