Latest web development tutorials

Visualización de datos XML en una página HTML

Visualización de datos XML en una página HTML

En el ejemplo siguiente, se abre un archivo XML ( " cd_catalog.xml "), y luego recorrer cada elemento de CD, y muestra el valor de un elemento HTML y elementos de tabla Nombre de artista:

Ejemplos

<html>
<body>

<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","cd_catalog.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML;

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("CD");
for (i=0;i<x.length;i++)
{
document.write("<tr><td>");
document.write(x[i].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
document.write("</td><td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
document.write("</td></tr>");
}
document.write("</table>");
</script>

</body>
</html>

Trate »

Para obtener más información sobre el uso de JavaScript y XML DOM, visite nuestro XML DOM tutorial .