Proprietà childNodes dell'XML DOM
Definizione e uso
childNodes
Questa proprietà restituisce un NodeList che contiene i nodi figli del nodo selezionato.
Se il nodo selezionato non ha nodi figli, questa proprietà restituisce un NodeList che non contiene nodi.
Suggerimento:Per esplorare la lista childNodes in modo ciclico, utilizzare l'attributo nextSibling è più efficace rispetto all'uso esplicito dell'elenco childNodes dell'oggetto padre.
Sintassi
elementNode.childNodes
Esempio
Esempio 1
Il codice seguente carica "books.xml" nella xmlDoc e ottiene il testo del primo elemento <title> in "books.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 xmlDoc = xml.responseXML; var x = xmlDoc.getElementsByTagName("title")[0]; var y = x.childNodes[0]; document.getElementById("demo").innerHTML = y.nodeValue; }
Esempio 2
Il codice seguente carica "books.xml" nella xmlDoc e ottiene il numero di nodi figli del primo elemento <book> in "books.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 xmlDoc = xml.responseXML; var x = xmlDoc.getElementsByTagName("book")[0].childNodes; document.getElementById("demo").innerHTML = x.length; }
Firefox e la maggior parte degli altri browser considerano spazi bianchi o newline come nodi di testo, mentre Internet Explorer non lo fa. Pertanto, l'output sarà diverso nell'esempio sopra.
Per ulteriori informazioni sulle differenze tra i browser, visitare la sezione DOM Browser del manuale XML DOM.