Elemento XML DOM () Método
❮ Objeto NamedNodeMap
Ejemplo
El siguiente fragmento de código carga " books.xml " en xmlDoc, recorre los elementos <book> e imprime los valores de los atributos de categoría:
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;
}
La salida del código anterior será:
cooking
children
web
web
Definición y uso
El método item() devuelve el nodo en el índice especificado en una lista de nodos.
Sintaxis
item(index)
Parameter | Description |
---|---|
index | The index |
Demostraciones para que lo pruebe usted mismo
❮ Objeto NamedNodeMap