Displaying XML data in HTML Table?

qais picture qais · Mar 27, 2012 · Viewed 10.3k times · Source

I tried to store some TV listings for a day into XML, make a DTD validation file for the TV listings, then make a plain HTML web page that turns the XML data into a table.

I'm pretty sure the problem lies in what I am doing with the HTML, or the structure of my XML tags. I know there is a Javascript method that may be better than what I've done in the HTML, but I was specifically requested to do it that way.

HTML : http://pastebin.com/1LgujZUj

Answer

Oscar Castiblanco picture Oscar Castiblanco · Mar 27, 2012

You can try using XSTL or a javascript script like:

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

http://www.w3schools.com/xml/xml_to_html.asp