HTML DOM Example 3
Catalog.html
<html>
<body>
<script
type="text/javascript">
var
xmlDoc=null;
if
(window.ActiveXObject)
{//
code for IE
xmlDoc=new
ActiveXObject("Microsoft.XMLDOM")
}
else
if (document. implementation.createDocument)
{//
code for Mozilla, Firefox, Opera, etc.
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your
browser cannot handle this script');
if (xmlDoc!=null)
{
xmlDoc.async=false;
xmlDoc.load("catalog.xml");
var
x=xmlDoc.getElementsByTagName("BOOK");
document.write("<table
border=”1”>);
document.write("<thead>");
document.write("<tr><th>Author</th><th>Book
Name</th></tr>");
document.write("</thead>");
document.write("<tfoot>");
document.write("<tr><th
colspan='2'>This is book catalog</th></tr>");
document.write("</tfoot>");
for
(var i=0;i<x.length;i++)
{
document.write("<tr>");
document.write("<td>");
document.write(x[i].getElementsByTagName("AUTHOR")[0].childNodes[0]
.nodeValue);
document.write("</td>");
document.write("<td>");
document.write(x[i].getElementsByTagName("TITLE")[0].childNodes[0]
.nodeValue);
document.write("</td>");
document.write("</tr>");
}
document.write("</table>");
}
</script>
</body> </html>
Catalog.xml
<?xml
version="1.0" encoding="IS0-8859-1"?>
<!--
Edited by XMLSpy® -->
<CATALOG>
<BOOK>
<TITLE>WEB
TECHNOLOGIES</TITLE>
<AUTHOR>CHRIS
BATES</AUTHOR>
<PRICE>40.50</PRICE>
<YEAR>1985</YEAR>
</BOOK>
<BOOK>
<TITLE>COMPUTER
NETWORKS</TITLE>
<AUTHOR>TANENBAUM</AUTHOR>
<PRICE>69.25</PRICE>
<YEAR>1988</YEAR>
</BOOK>
<BOOK>
<TITLE>ACD</TITLE>
<AUTHOR>AHOULLMAN</AUTHOR>
<PRICE>135.90</PRICE>
<YEAR>1982</YEAR>
</BOOK>
<BOOK>
<TITLE>NETWORK
MANAGEMENT</TITLE>
<AUTHOR>MANI
SUBRAMANIANCAUTHOR>
<PRICE>110.20</PRICE>
<YEAR>1990</YEAR>
</BOOK>
<BOOK>
<TITLE>DCN</TITLE>
<AUTHOR>FOROUZAN</AUTHOR>
<PRICE>120.90</PRICE>
<YEAR>1997</YEAR>
</BOOK>
<BOOK>
<TITLE>JAVA</TITLE>
<AUTHOR>NAUGHTON</AUTHOR>
<PRICE>160.90</PRICE>
<YEAR>1998</YEAR>
</BOOK>
</CATALOG>
Output: