Método XML DOM item()
❮ Objeto NamedNodeMap
Exemplo
O fragmento de código a seguir carrega " books.xml " em xmlDoc, percorre os elementos <book> e imprime os valores dos atributos de 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;
}
A saída do código acima será:
cooking
children
web
web
Definição e uso
O método item() retorna o nó no índice especificado em uma lista de nós.
Sintaxe
item(index)
Parameter | Description |
---|---|
index | The index |
Demonstrações de teste você mesmo
❮ Objeto NamedNodeMap