XML DOM childNodes
Property
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
that does not contain any nodes.
Syntax
Tips and Notes
Tip: To loop through the childNodes
list, using the nextSibling
property is more efficient than using the childNodes
list of the parent object.
Example 1
The following code snippet uses loadXMLDoc() to load "books.xml" into xmlDoc
and retrieves the text node from the first <title>
element in "books.xml":
Example
The above code will output:
Example 2
The following code snippet uses loadXMLDoc() to load "books.xml" into xmlDoc
and retrieves the number of child nodes from the first <title>
element in "books.xml":
Example
In Internet Explorer, the above code will output:
In Mozilla browsers, the above code will output:
Firefox and most other browsers treat spaces or line breaks between nodes as text nodes, while Internet Explorer ignores whitespace text nodes generated between nodes. Therefore, the output differs in the above example.
For more information on browser differences, visit our DOM Browsers section in the XML DOM tutorial.