Accessing the XML DOM
One of the great things about XML is the possibility to separate presentation
(HTML) from the data.
Providing HTML Content From XML Files
One of the great things about XML is the possibility to separate presentation
(HTML) from the data.
By using the XML parser, an HTML
page can be constructed as a static document, and an embedded JavaScript can be
used to
provide dynamic data.
The following example reads data from an XML document and writes the XML data
into (waiting) HTML elements:
Cross Browser Example
<html>
<head>
<script type="text/javascript">
var xmlDoc;
function loadXML()
{
//load xml file
// code for IE
if (window.ActiveXObject)
{
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("note.xml");
getmessage();
}
// code for Mozilla, etc.
else if (document.implementation &&
document.implementation.createDocument)
{
xmlDoc= document.implementation.createDocument("","",null);
xmlDoc.load("note.xml");
xmlDoc.onload=getmessage;
}
else
{
alert('Your browser cannot handle this script');
}
}
function getmessage()
{
document.getElementById("to").innerHTML=
xmlDoc.getElementsByTagName("to")[0].firstChild.nodeValue;
document.getElementById("from").innerHTML=
xmlDoc.getElementsByTagName("from")[0].firstChild.nodeValue;
document.getElementById("message").innerHTML=
xmlDoc.getElementsByTagName("body")[0].firstChild.nodeValue;
}
</script>
</head>
<body onload="loadXML()" bgcolor="yellow">
<h1>W3Schools Internal Note</h1>
<p><b>To:</b> <span id="to"></span><br />
<b>From:</b> <span id="from"></span>
<hr />
<b>Message:</b> <span id="message"></span>
</p>
</body>
</html>
|
Try
it yourself
Try it yourself
with VBScript
Important: To extract the text (Jani) from an element
like this: <from>Jani</from>, the syntax is: getElementsByTagName("from")[0].firstChild.nodeValue,
and NOT like this: getElementsByTagName("from").nodeValue. The reason for
this is that the result returned
from getElementsByTagName is an array of nodes, containing all the nodes
within the XML document with the specified tag name (in this case "from").
Traversing the Node-tree
With VBScript, it is very simple to extract elements (and their value) from an XML document
by traversing the node-tree:
<script type="text/vbscript">
set xmlDoc=CreateObject("Microsoft.XMLDOM")
xmlDoc.async="false"
xmlDoc.load("note.xml")
for each x in xmlDoc.documentElement.childNodes
document.write("<b>" & x.nodename & ":</b> ")
document.write(x.text)
document.write("<br />")
next
</script> |
Try it yourself and
also try to traverse our
CD
catalog example.
Learn XML with <oXygen/> XML Editor - Free Trial!
 |
|
oXygen helps you learn to define,
edit, validate and transform XML documents. Supported technologies include XML Schema,
DTD, Relax NG, XSLT, XPath, XQuery, CSS.
Understand in no time how XSLT and XQuery work by using the intuitive oXygen debugger!
Do you have any XML related questions? Get free answers from the oXygen
XML forum
and from the video
demonstrations.
Download a FREE 30-day trial today!
|
|