XML DOM childNodes Property
Definition and Usage
childNodes
The property returns a NodeList of child nodes for the specified node.
Tip:You can use the length property to determine the number of child nodes, then loop through all child nodes and extract the information you want.
Syntax
nodeObject.childNodes
Technical Details
Return value: | The NodeList object representing a set of nodes. |
---|---|
DOM Version: | Core Level 1 |
Instance
Example 1
The following code loads "books.xml" into xmlDoc and displays the child nodes of the XML document:
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 x, i, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.childNodes; for (i = 0; i < x.length; i++) { txt += "Nodename: " + x[i].nodeName + " (nodetype: " + x[i].nodeType + ")"; } document.getElementById("demo").innerHTML = txt; }
Example 2
Display all child nodes of all elements in the XML document:
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 x, y, i, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.documentElement; y = x.childNodes; for(i = 0; i < y.length; i++) { txt += "Nodename: " + y[i].nodeName + " (nodetype: " + y[i].nodeType + ")<br>"; for(z = 0; z < y[i].childNodes.length; z++) { txt += "Nodename: " + y[i].childNodes[z].nodeName + " (nodetype: " + y[i].childNodes[z].nodeType + ")<br>"; } } document.getElementById("demo").innerHTML = "Nodename: " + xmlDoc.nodeName + " (nodetype: " + xmlDoc.nodeType + ")<br>" "Nodename: " + x.nodeName + "}} " (nodetype: " + x.nodeType + ")<br>" txt; }
Browser Support
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
Support | Support | Support | Support | Support |
All major browsers support childNodes
Properties.