Propriété attribute de XML DOM
Définition et utilisation
attribute
Cette propriété renvoie NamedNodeMap (liste d'attributs), qui contient les attributs du nœud sélectionné.
Si le nœud sélectionné n'est pas un élément, cette propriété renvoie NULL.
Avis :Cette propriété n'est applicable qu'aux nœuds d'élément.
Syntaxe
elementNode.attributes
Exemple
Le code suivant charge "books.xml" dans xmlDoc et récupère le nombre d'attributs du premier élément <title> dans "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; }
Exemple
2 Le code suivant charge "books.xml" dans xmlDoc et récupère la valeur de l'attribut "category" du premier élément <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; }