Propriedade textContent do XML DOM
Definição e uso
textContent
Atributo define ou retorna o conteúdo de texto do nó e de seus descendentes.
Quando definido, todos os nós filhos serão removidos e substituídos por um único nó de texto que contém este valor de atributo.
Sintaxe
nodeObject.textContent
Exemplo
Exemplo 1
O código seguinte carrega "books.xml" para xmlDoc e retorna o conteúdo de texto do 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; }
Exemplo 2
Definir o conteúdo de texto do nó:
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'); // Definir textContent for(i = 0; i < x.length; i++) { x.item(i).textContent = "Desatualizado"; } // Imprimir textContent for(i = 0; i < x.length; i++) { txt += x.item(i).textContent + "<br>"; } document.getElementById("demo").innerHTML = txt; }