XML DOM attribute özelliği

Tanım ve Kullanım

attribute Özellik, seçilen düğümün özellik listesini içeren NamedNodeMap döner.

Seçilen düğüm element değilse, bu özellik NULL döner.

İpucu:Bu özellik yalnızca element düğümleri için geçerlidir.

Dilbilgisi

elementNode.attributes

Örnek

Aşağıdaki kod, "books.xml" dosyasını xmlDoc'a yükler ve "books.xml" dosyasındaki ilk <title> elementinin ö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")[0].attributes;
    document.getElementById("demo").innerHTML =
    x.length;
}

Kişisel Deneyim

Örnek

2 Aşağıdaki kod, "books.xml" dosyasını xmlDoc'a yükler ve ilk <book> elementinin "category" özelliğinin değerini 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 x, i, att, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName('book');
    for (i = 0; i < x.length; i++) {
        att = x.item(i).attributes.getNamedItem("category");
        txt += att.value + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

Kişisel Deneyim