Proprietà textContent dell'XML DOM

Definizione e uso

textContent Impostazione o restituzione del contenuto testuale del nodo e dei suoi discendenti.

Quando impostato, tutti i nodi figli vengono eliminati e sostituiti da un singolo nodo di testo che contiene questo valore dell'attributo.

Sintassi

nodeObject.textContent

Esempio

Esempio 1

Il seguente codice carica "books.xml" nel xmlDoc e restituisce il contenuto testuale dell'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;
}

Prova personalmente

Esempio 2

Impostare il contenuto testuale 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');
    // Impostare textContent
    for(i = 0; i < x.length; i++) {
        x.item(i).textContent = "Obsoleto";
    }
    // Output di textContent
    for(i = 0; i < x.length; i++) {
        txt += x.item(i).textContent + "<br>";
    }
    document.getElementById("demo").innerHTML = txt;
}

Prova personalmente