Metodo XML DOM getNamedItem()
❮ Oggetto NamedNodeMap
Esempio
Il seguente frammento di codice carica " books.xml " in xmlDoc, scorre gli elementi <book> e stampa i valori degli attributi di categoria:
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;
}
L'output del codice sopra sarà:
cooking
children
web
web
Definizione e utilizzo
Il metodo getNamedItem() restituisce il nodo con il nome specifico (da una namedNodeMap).
Supporto del browser
Il metodo getNamedItem() è supportato in tutti i principali browser.
Nota: Internet Explorer 8 e versioni precedenti non supportano questo metodo.
Sintassi
namedNodeMap.getNamedItem(nodeName)
Parametri
Parameter | Type | Description |
---|---|---|
nodeName | String | Required. The name of the node to return |
Valore di ritorno
Type | Description |
---|---|
Node object | The node with the specified name, , or null if it does not identify any node in the map |
Dettagli tecnici
Versione DOM | Livello base 1 |
---|
Dimostrazioni fai-da-te
❮ Oggetto NamedNodeMap