XML DOM
XML DOM
Cos'è il DOM?
Il DOM definisce uno standard per l'accesso e la manipolazione dei documenti:
Il DOM HTML definisce un modo standard per accedere e manipolare documenti HTML. Presenta un documento HTML come una struttura ad albero.
Il DOM XML definisce un modo standard per accedere e manipolare documenti XML. Presenta un documento XML come una struttura ad albero.
Comprendere il DOM è un must per chiunque lavori con HTML o XML.
Il DOM HTML
Tutti gli elementi HTML sono accessibili tramite il DOM HTML.
Questo esempio cambia il valore di un elemento HTML con id="demo":
Esempio
<h1 id="demo">This is a Heading</h1>
<button type="button"
onclick="document.getElementById('demo').innerHTML =
'Hello World!'">Click Me!
</button>
Puoi imparare molto di più sul DOM HTML nel nostro tutorial JavaScript .
Il DOM XML
È possibile accedere a tutti gli elementi XML tramite il DOM XML.
Libri.xml
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="cooking">
<title lang="en">Everyday
Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry
Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
</bookstore>
Questo codice recupera il valore di testo del primo elemento <title> in un documento XML:
Esempio
txt = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
Il DOM XML è uno standard su come ottenere, modificare, aggiungere ed eliminare elementi XML.
Questo esempio carica una stringa di testo in un oggetto DOM XML ed estrae le informazioni da esso con JavaScript:
Esempio
<html>
<body>
<p id="demo"></p>
<script>
var text, parser,
xmlDoc;
text = "<bookstore><book>" +
"<title>Everyday
Italian</title>" +
"<author>Giada De Laurentiis</author>" +
"<year>2005</year>" +
"</book></bookstore>";
parser = new DOMParser();
xmlDoc = parser.parseFromString(text,"text/xml");
document.getElementById("demo").innerHTML =
xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
</script>
</body>
</html>
Imparerai molto di più sull'XML DOM nel nostro XML DOM Tutorial .