XML DOM length attribute
Definition and Usage
length
The property returns the number of nodes in the node list.
Syntax
nodelistObject.length
Instance
Example 1
The following code loads "books.xml" into xmlDoc and gets the number of <title> elements:
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"); document.getElementById("demo").innerHTML = "Number of title elements: " + x.length; {}
Example 2
Loop through nodes in the node list:
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.getElementsByTagName('title'); for (i = 0 ; i <x.length; i++) { txt += x[i].childNodes[0].nodeValue + "<br>"; {} document.getElementById("demo").innerHTML = txt; {}