Wednesday, October 7, 2009

XML to HTML

Hi,
How can we convert XML datas into an HTML file?
Simple.
Step1:
Name the xml files as example.xml
Step2: The xml data should be like this. There will be CD tag which is used for conversion.

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<CATALOG>
<CD>
<TITLE>Empire Burlesque</TITLE>
<ARTIST>Bob Dylan</ARTIST>
<COUNTRY>USA</COUNTRY>
<COMPANY>Columbia</COMPANY>
<PRICE>10.90</PRICE>
<YEAR>1985</YEAR>
</CD>

Step3: Create an HTML file as test.html

<html>
<body>

<script type="text/javascript">
if (window.XMLHttpRequest)
{
xhttp=new window.XMLHttpRequest();
}
else // Internet Explorer 5/6
{
xhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET","example.xml",false);
xhttp.send("");
xmlDoc=xhttp.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>

Step5: Run the test.html file,,and see the XML datas inside the CD tag will be converted into HTML table.
For more details... Here is the link

38 comments:

  1. thanks god for the chance to enjoy so many good artical.........................................

    ReplyDelete
  2. 失去金錢的人,失去很多;失去朋友的人,失去更多;失去信心的人,失去所有。..................................................

    ReplyDelete
  3. 開懷幸福的生活,是每個人的夢想~~希望大家都能夠實現!..............................

    ReplyDelete
  4. 失去金錢的人,失去很多;失去朋友的人,失去更多;失去信心的人,失去所有。..................................................................

    ReplyDelete
  5. 字是活的,人和環境的觀察是活的,思想是活的。不管怎樣,就是要有一兩樣是活的。否則都是平庸。............................................................

    ReplyDelete
  6. 不願彎腰撿拾一根針的人,永遠不值得一塊錢。..................................................

    ReplyDelete
  7. 堅持是為著某種目的或目標,而持續不斷朝向既定方向努力的一種意念。..................................................

    ReplyDelete
  8. 唯有學習不已的老師,才能認真的教,唯有燃燒自己,才能點亮他人的燈............................................................

    ReplyDelete