PDA

View Full Version : Add HTML to XML Data



JizzaBeez
03-03-2011, 07:43 AM
Add HTML to XML Data

In the following example, we loop through an XML file ("cd_catalog.xml (http://www.w3schools.com/xml/cd_catalog.xml)"), and display the contents of each CD element as an HTML table row:
Example


<html>
<body>

<script type="text/javascript">
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>Try it yourself ยป (http://www.w3schools.com/xml/tryit.asp?filename=tryxml_display_table)

JohnnyCbad
05-17-2012, 07:25 PM
Very nice simple tutorial, good work!