Atributo de attribute XML DOM
Definición y uso
attribute
La propiedad devuelve NamedNodeMap (lista de atributos), que contiene los atributos del nodo seleccionado.
Si el nodo seleccionado no es un nodo de elemento, esta propiedad devuelve NULL.
Consejo:Esta propiedad solo se aplica a los nodos de elemento.
Sintaxis
elementNode.attributes
Ejemplo
El siguiente código carga "books.xml" en xmlDoc y obtiene la cantidad de atributos del primer elemento <title> en "books.xml":
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 xmlDoc = xml.responseXML; var x = xmlDoc.getElementsByTagName("book")[0].attributes; document.getElementById("demo").innerHTML = x.length; }
Ejemplo
2 El siguiente código carga "books.xml" en xmlDoc y obtiene el valor del atributo "category" del primer elemento <book>: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; }