Metodo XML DOM item()
❮ 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 item() restituisce il nodo all'indice specificato in un elenco di nodi.
Sintassi
item(index)
Parameter | Description |
---|---|
index | The index |
Dimostrazioni fai-da-te
❮ Oggetto NamedNodeMap