Displaying XML with JavaScript
This example focus on the XML software implemented in Microsoft Internet Explorer 5, and its XML processor called MSXML. It will be of little use to explore any of our software examples using any other browser.
To display XML data inside an HTML page you can use JavaScript to import data from an XML file. To see how XML and HTML complement each other this way; first let us create the XML document (note.xml), then create the HTML document (note.htm) that contains a JavaScript which reads an XML file and displays the information inside the HTML page
Open your favorite editor and type the following and name it as note.xml
<?xml version="1.0" encoding="ISO8859-1" ?>
- <note>
<to>Anuradha</to>�
<from>IQResearch</from>�
<heading>Well done</heading>�
<body>Congrats ! You have done excellent job.</body>�
</note>
Open your favorite editor and type the following and name it as
note.htm
<html>
<head>
<script language="JavaScript"
for="window" event="onload">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
nodes = xmlDoc.documentElement.childNodes
to.innerText = nodes.item(0).text
from.innerText = nodes.item(1).text
header.innerText = nodes.item(2).text
body.innerText = nodes.item(3).text
</script>
<title>HTML using XML data</title>
</head>
<body bgcolor="yellow">
<h1>Refsnes Data Internal Note</h1>
<b>To: </b><span id="to"></span>
<br>
<b>From: </b><span id="from"></span>
<hr>
<b><span id="header"></span></b>
<hr>
<span id="body"></span>
</body>
</html>
That is all.....