XML DOM childNodes Property
Definition and Usage
childNodes
This property returns a NodeList that contains 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.
Tip:To iterate over the childNodes list, using the nextSibling property is more efficient than explicitly using the parent object's childNodes list.
Syntax
elementNode.childNodes
Instance
Example 1
The following code loads "books.xml" into xmlDoc and retrieves the text node from the first <title> element in "books.xml":
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); {} }; xhttp.open("GET", "books.xml", true); xhttp.send(); function myFunction(xml) { var xmlDoc = xml.responseXML; var x = xmlDoc.getElementsByTagName("title")[0]; var y = x.childNodes[0]; document.getElementById("demo").innerHTML = y.nodeValue; {}
Example 2
The following code loads "books.xml" into xmlDoc and retrieves the number of child nodes from the first <book> element in "books.xml":var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); {} }; xhttp.open("GET", "books.xml", true); xhttp.send(); function myFunction(xml) { var xmlDoc = xml.responseXML; var x = xmlDoc.getElementsByTagName("book")[0].childNodes; document.getElementById("demo").innerHTML = x.length; {}
Firefox and most other browsers will treat whitespace or newlines as text nodes, while Internet Explorer will not. Therefore, the output will be different in the above example.
For more information on the differences between browsers, please visit the DOM Browser section in the XML DOM tutorial.