Atributo data do XML DOM
Definição e Uso
data
Define ou retorna o texto do comentário.
Sintaxe
commentNode.data
Exemplo
A seguir, o código carregará "books_comment.xml" para xmlDoc e exibirá o texto de comentário do primeiro elemento <book>:
var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { myFunction(this); } }; xhttp.open("GET", "books_comment.xml", true); xhttp.send(); function myFunction(xml) { var x, i, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName("book")[0].childNodes; for (i = 0; i < x.length; i++) { if (x[i].nodeType == 8) { txt += x[i].data + "<br>"; } } document.getElementById("demo").innerHTML = txt; }
No exemplo acima, usamos loops e语句 para garantir que tratemos apenas nós de comentário. O tipo de nó do nó de comentário é 8.