Proprietà childNodes del DOM XML
Definizione e uso
childNodes
L'attributo restituisce un NodeList che rappresenta i figli del nodo specificato.
Suggerimento:Puoi utilizzare l'attributo length per determinare il numero di nodi figli, quindi eseguire un ciclo per esplorare tutti i nodi figli e estrarre le informazioni desiderate.
Sintassi
nodeObject.childNodes
Dettagli tecnici
Valore di ritorno: | Rappresenta un oggetto NodeList che rappresenta un insieme di nodi. |
---|---|
Versione DOM: | Core Livello 1 |
Esempio
Esempio 1
Il seguente codice carica "books.xml" in xmlDoc e visualizza i nodi figli del documento XML:
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.childNodes; for (i = 0; i < x.length; i++) { txt += "Nodename: " + x[i].nodeName + "(nodetype: " + x[i].nodeType + ")"; } document.getElementById("demo").innerHTML = txt; }
Esempio 2
Visualizza tutti i nodi figli di tutti gli elementi dell'XML documento:
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, y, i, xmlDoc, txt; xmlDoc = xml.responseXML; txt = ""; x = xmlDoc.documentElement; y = x.childNodes; for(i = 0; i < y.length; i++) { txt += "Nodename: " + y[i].nodeName + "(nodetype: " + y[i].nodeType + ")<br>"; for(z = 0; z < y[i].childNodes.length; z++) { txt += "Nodename: " + y[i].childNodes[z].nodeName + "(nodetype: " + y[i].childNodes[z].nodeType + ")<br>"; } } document.getElementById("demo").innerHTML = "Nodename: " + xmlDoc.nodeName + "(nodetype: " + xmlDoc.nodeType + ")<br>" "Nodename: " + x.nodeName +" " (nodetype: " + x.nodeType + ")<br>" + txt; }
浏览器支持
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
支持 | 支持 | 支持 | 支持 | 支持 |
所有主流浏览器都支持 childNodes
属性。