Propriedade attribute do XML DOM
Definição e uso
attribute
A propriedade retorna NamedNodeMap (lista de atributos), que contém os atributos do nó selecionado.
Se o nó selecionado não for um nó de elemento, esta propriedade retorna NULL.
Dica:Esta propriedade é aplicável apenas a nós de elemento.
Sintaxe
elementNode.attributes
Exemplo
O código a seguir carrega "books.xml" para xmlDoc e obtém o número de atributos do primeiro elemento <title> dentro de "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; {}
Exemplo
2 O código a seguir carrega "books.xml" para xmlDoc e obtém o valor da propriedade "category" do primeiro 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; {}