Latest web development tutorials

DOM XML

DOM (Document Object Model Document Object Model) define una forma estándar para acceder y manipular el documento.


DOM XML

DOM XML (XML Document Object Model) define un método estándar para acceder y manipular documentos XML.

XML documento XML DOM como una vista de árbol.

Todos los elementos se puede acceder a través del árbol DOM. Puede modificar o eliminar sus contenidos, y para crear nuevos elementos. Elementos, su texto, y sus propiedades, se considera que un nodo.

En nuestro XML DOM tutorial , usted puede aprender más sobre el DOM XML.


HTML DOM

HTML DOM define una manera estándar para acceder y manipular documentos HTML.

Todos los elementos HTML se puede acceder a través del DOM HTML.

En nuestro HTML DOM tutorial , se puede aprender más acerca del conocimiento de HTML DOM. .


Carga de un archivo XML - instancia entre navegadores

El documento XML siguiente ejemplo ( " note.xml ") para analizar XML DOM objeto y, a continuación, para extraer algo de información a través de JavaScript:

Ejemplos

<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>

Trate »


Nota importante!

Porque desde el principio del archivo XML ( "note.xml") del elemento <to> extrae el texto "Tove", la sintaxis es:

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

Tenga en cuenta que aunque el archivo XML contiene un único elemento <to>, aún tiene que especificar el índice de matriz [0]. Esto es porque el método getElementsByTagName () devuelve una matriz.


Cargando una cadena XML - Cross-browser instancia

Los siguientes ejemplos de la cadena de XML en el objeto DOM XML, y luego para extraer algo de información a través de JavaScript:

Ejemplos

<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>

Trate »