0

The following is the code for XML FILE :

<?xml version="1.0"?>
<catalog>
   <book id="bk101">
      <author>Gambardella, Matthew</author>
      <title>XML Developer's Guide</title>
      <genre>Computer</genre>
      <price>44.95</price>
      <publish_date>2000-10-01</publish_date>
      <description>An in-depth look at creating applications 
      with XML.</description>
   </book>
</catalog>

Need to display the fields in a tabular format in HTML5.

feeela
  • 29,399
  • 7
  • 59
  • 71
Ishmeet Kalsi
  • 131
  • 1
  • 2
  • 12
  • 1
    Please follow this [URL to Parse the XML](http://stackoverflow.com/questions/10852493/need-read-xml-and-show-it-and-save-on-local-storage) and store locally (if you require). – AppMobiGurmeet Oct 11 '12 at 08:23

1 Answers1

2

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

working example: http://kasperkoman.com/xmltohtml/

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

document.write("<table border='1'>");
var x=xmlDoc.getElementsByTagName("book");
for (i=0;i<x.length;i++)
  { 
  document.write("<tr><td>");
  document.write(x[i].getElementsByTagName("author")[0].childNodes[0].nodeValue);
  document.write("</td><td>");
  document.write(x[i].getElementsByTagName("title")[0].childNodes[0].nodeValue);
  document.write("</td><td>");
  document.write(x[i].getElementsByTagName("genre")[0].childNodes[0].nodeValue);
  document.write("</td><td>");
  document.write(x[i].getElementsByTagName("price")[0].childNodes[0].nodeValue);
  document.write("</td><td>");
  document.write(x[i].getElementsByTagName("publish_date")[0].childNodes[0].nodeValue);
  document.write("</td><td>");
  document.write(x[i].getElementsByTagName("description")[0].childNodes[0].nodeValue);
  document.write("</td></tr>");
  }
document.write("</table>");
kapoko
  • 938
  • 1
  • 11
  • 29
  • kasper thanx for the help i also tried the same code from w3schools but it is not working on chrome and iE7. – Ishmeet Kalsi Oct 11 '12 at 09:01
  • Sorry, there were some mistakes in the code, the table tags were not written correctly. Now it should work. I've hosted an example here: http://kasperkoman.com/xmltohtml/ (I added one more entry to the xml file for example purposes). Check out the source! – kapoko Oct 11 '12 at 09:53