Método XML DOM getNamedItem()
❮ 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 getNamedItem() devuelve el nodo con el nombre específico (de un namedNodeMap).
Compatibilidad con navegador
El método getNamedItem() es compatible con todos los navegadores principales.
Nota: Internet Explorer 8 y versiones anteriores no admiten este método.
Sintaxis
namedNodeMap.getNamedItem(nodeName)
Parámetros
Parameter | Type | Description |
---|---|---|
nodeName | String | Required. The name of the node to return |
Valor devuelto
Type | Description |
---|---|
Node object | The node with the specified name, , or null if it does not identify any node in the map |
Detalles técnicos
Versión DOM | Nivel básico 1 |
---|
Demostraciones para que lo pruebe usted mismo
❮ Objeto NamedNodeMap