Propiedad textContent del XML DOM
Definición y uso
textContent
Propiedad de establecimiento o devolución del contenido de texto del nodo y de sus descendientes.
Cuando se establece, todos los nodos hijos se eliminan y se reemplazan por un solo nodo de texto que contiene este valor de atributo.
Sintaxis
nodeObject.textContent
Ejemplo
Ejemplo 1
El siguiente código carga "books.xml" en xmlDoc y devuelve el contenido de texto del 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, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName('book'); for(i = 0; i < x.length; i++) { txt += x.item(i).textContent + "<br>"; } document.getElementById("demo").innerHTML = txt; }
Ejemplo 2
Establecer el contenido de texto del nodo:
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, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.getElementsByTagName('book'); // Establecer textContent for(i = 0; i < x.length; i++) { x.item(i).textContent = "Outdated"; } // Salida de textContent for(i = 0; i < x.length; i++) { txt += x.item(i).textContent + "<br>"; } document.getElementById("demo").innerHTML = txt; }