XML DOM childNodes Property

Definition and Usage

childNodes The property returns a NodeList of the document's child nodes.

Tip:Use the NodeList's length property to determine the number of nodes in the node list. When we get the length of the node list, we can easily loop through it and extract the desired values!

Syntax

documentObject.childNodes

Example

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;
}

Try It Yourself