XML DOM length özelliği

Tanım ve Kullanım

length Özellik, NamedNodeMap içindeki düğüm sayısını döndürür.

Sözdizimi

nodemapObject.length

Örnek

Örnek 1

Aşağıdaki kod "books.xml" dosyasını xmlDoc içine yükler ve ilk <book> elemanındaki özellik sayısını alır:

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");
    document.getElementById("demo").innerHTML =
    "İlk <book> elemanındaki özellik sayısı: " +
    x.item(0).attributes.length;
}

Kişisel olarak deneyin

Örnek 2

Düğüm listesindeki düğüm sayısını döndür:

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 =
    "Başlık elemanlarının sayısı: " + x.length;
}

Kişisel olarak deneyin

Örnek 3

Döngü içinde düğüm listesindeki düğümleri gezin:

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

Kişisel olarak deneyin