XML DOM getNamedItem() metodu

tanım ve kullanım

getNamedItem() metod, belirli bir adın sahip olan nodu (namedNodeMap'dan gelen) döndürür.

gramer

namedNodeMap.getNamedItem(nodeName)

Parametreler

Parametreler Açıklama
nodeName Gerekli. String. Dönüştürmek istediğiniz düğümün adı.

Teknik ayrıntılar

DOM sürümü: Core Level 1
Dönüş değeri:

Node nesnesi. Belirtilen adı taşıyan bir düğüm.

Harita içinde herhangi bir düğüm işaretlenmemişse null döner.

Örnek

Aşağıdaki kod "books.xml" dosyasını xmlDoc içine yükler, <book> öğelerini döngüleyerek category özniteliğinin değerlerini yazdırı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;
}

Kendi Kendine Deneyin

Örnek 2

Özniteliğin değerini değiştirme:

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, attlist, att, xmlDoc, txt;
    xmlDoc = xml.responseXML;
    txt = "";
    x = xmlDoc.getElementsByTagName("book");
    // category özniteliğinin özniteliğinin değerini değiştir
    for (i = 0; i < x.length; i++) { 
        attlist = x.item(i).attributes;
        att = attlist.getNamedItem("category");
        att.value = "BESTSELLER";
    }
    // Çıktı tüm title ve edition
    for (i = 0; i < x.length; i++) { 
        txt += x[i].getAttribute("category") + "<br>";
    }
    document.getElementById("demo").innerHTML = txt; 
}

Kendi Kendine Deneyin

Tarayıcı Desteği

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Desteklenir Desteklenir Desteklenir Desteklenir Desteklenir

Tüm popüler tarayıcılar destekler getNamedItem() Yöntem:

Açıklama:Internet Explorer 8 ve daha eski sürümler bu yöntemi desteklememektedir.