Propriedade XML DOM textContent
❮ Objeto de nó
Exemplo
O fragmento de código a seguir carrega " books.xml " em xmlDoc e retorna o conteúdo textual dos elementos <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;
}
A saída do código acima será:
Everyday Italian Giada De Laurentiis 2005 30.00
Harry Potter J K. Rowling 2005 29.99
XQuery Kick Start James McGovern Per Bothner Kurt Cagle James Linn
Vaidyanathan Nagarajan 2003 49.99
Learning XML Erik T. Ray 2003 39.95
Definição e uso
A propriedade textContent define ou retorna o conteúdo textual de um nó e seus descendentes.
Na configuração, todos os nós filhos são removidos e substituídos por um único nó Text contendo a string para a qual esta propriedade está configurada.
Sintaxe
nodeObject.textContent
Demonstrações de teste você mesmo
❮ Objeto de nó