Latest web development tutorials

DOM XML

DOM (Document Object Model Document Object Model) definisce un modo standard per accedere e manipolare il documento.


DOM XML

DOM XML (XML Document Object Model) definisce un metodo standard per l'accesso e la manipolazione di documenti XML.

XML documento XML DOM come una struttura ad albero.

Tutti gli elementi sono accessibili attraverso l'albero DOM. È possibile modificare o cancellare i suoi contenuti, e per creare nuovi elementi. Elementi, loro testo, e le loro proprietà, sono considerati un nodo.

Nel nostro DOM XML esercitazione , si può imparare di più sul DOM XML.


HTML DOM

HTML DOM definisce un modo standard per l'accesso e la manipolazione di documenti HTML.

Tutti gli elementi HTML possono essere accessibili tramite il DOM HTML.

Nel nostro HTML DOM esercitazione , è possibile saperne di più su conoscenze HTML DOM. .


Caricare un file XML - cross-browser esempio

Il documento XML seguente esempio ( " note.xml ") per analizzare oggetto DOM XML, e quindi di estrarre alcune informazioni tramite JavaScript:

Esempi

<html>
<body>
<h1>W3Schools Internal Note</h1>
<div>
<b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
</div>

<script>
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","note.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>

</body>
</html>

Prova »


Nota importante!

Dalla cima del file XML ( "note.xml") di <a> elemento estrae il testo "Tove", la sintassi è:

getElementsByTagName("to")[0].childNodes[0].nodeValue

Si noti che anche file XML contiene un solo <a> elemento, è comunque necessario specificare l'indice array [0]. Questo perché il metodo getElementsByTagName () restituisce un array.


Caricamento di una stringa XML - Cross-Browser esempio

I seguenti esempi della stringa XML parsing l'oggetto DOM XML, e quindi per estrarre alcune informazioni tramite JavaScript:

Esempi

<html>
<body>
<h1>W3Schools Internal Note</h1>
<div>
<b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span><br />
<b>Message:</b> <span id="message"></span>
</div>

<script>
txt="<note>";
txt=txt+"<to>Tove</to>";
txt=txt+"<from>Jani</from>";
txt=txt+"<heading>Reminder</heading>";
txt=txt+"<body>Don't forget me this weekend!</body>";
txt=txt+"</note>";

if (window.DOMParser)
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
}
else // Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.loadXML(txt);
}

document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
</script>
</body>
</html>

Prova »