Metodo getNamedItem() di XML DOM

Definizione e uso

getNamedItem() Il metodo restituisce il nodo con nome specifico (da namedNodeMap).

sintassi

namedNodeMap.getNamedItem(nodeName)

Parameters

Parameters Description
nodeName Required. String. The name of the node to return.

Technical details

DOM version: Core Level 1
Return value:

Node object. Has a node with the specified name.

If no node is marked in the mapping, then return null.

Example

The following code loads "books.xml" into xmlDoc, loops through the <book> elements, and prints the value of the category attribute:

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

Prova personalmente

Example 2

Change the value of the attribute:

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");
    // Change the attribute value of the category attribute
    for (i = 0; i < x.length; i++) { 
        attlist = x.item(i).attributes;
        att = attlist.getNamedItem("category");
        att.value = "BESTSELLER";
    }
    // Output all title and edition
    for (i = 0; i < x.length; i++) { 
        txt += x[i].getAttribute("category") + "<br>";
    }
    document.getElementById("demo").innerHTML = txt; 
}

Prova personalmente

Supporto del browser

Chrome Edge Firefox Safari Opera
Chrome Edge Firefox Safari Opera
Supporto Supporto Supporto Supporto Supporto

Tutti i browser principali lo supportano getNamedItem() Metodo.

Nota:Internet Explorer 8 e versioni precedenti non supportano questo metodo.