Atributo data del XML DOM

Definición y uso

data Establece o devuelve el texto de la anotación.

Sintaxis

commentNode.data

Ejemplo

El siguiente código cargará "books_comment.xml" en xmlDoc y mostrará el texto de comentario del primer 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;
}

Prueba personalmente

En el ejemplo anterior, utilizamos bucles y sentencias if para asegurarnos de que solo procesamos nodos de comentarios. El tipo de nodo de los nodos de comentarios es 8.