DOM XML
DOM XML
¿Qué es el DOM?
El DOM define un estándar para acceder y manipular documentos:
El HTML DOM define una forma estándar para acceder y manipular documentos HTML. Presenta un documento HTML como una estructura de árbol.
El DOM XML define una forma estándar para acceder y manipular documentos XML. Presenta un documento XML como una estructura de árbol.
Comprender el DOM es imprescindible para cualquier persona que trabaje con HTML o XML.
El DOM HTML
Se puede acceder a todos los elementos HTML a través del HTML DOM.
Este ejemplo cambia el valor de un elemento HTML con id="demo":
Ejemplo
<h1 id="demo">This is a Heading</h1>
<button type="button"
onclick="document.getElementById('demo').innerHTML =
'Hello World!'">Click Me!
</button>
Puede aprender mucho más sobre HTML DOM en nuestro tutorial de JavaScript .
El DOM XML
Se puede acceder a todos los elementos XML a través del XML DOM.
Libros.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>
Este código recupera el valor de texto del primer elemento <title> en un documento XML:
Ejemplo
txt = xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue;
El XML DOM es un estándar sobre cómo obtener, cambiar, agregar y eliminar elementos XML.
Este ejemplo carga una cadena de texto en un objeto DOM XML y extrae la información con JavaScript:
Ejemplo
<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>
Aprenderá mucho más sobre XML DOM en nuestro Tutorial de XML DOM .