|
XML DOM Load Functions
The code for loading XML documents can be stored in a function.
The loadXMLDoc() Function
To make the code
from the previous page simpler to maintain (and check for older browsers), it should be written as a function:
function loadXMLDoc(dname)
{
var xmlDoc;
if (window.XMLHttpRequest)
{
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET",dname,false);
xmlDoc.send("");
return xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XMLDOM"))
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load(dname);
return xmlDoc;
}
alert("Error loading document");
return null;
}
|
The function above can be stored in the <head> section of an HTML page, and called from a script in the page.
The function described above, is used in all
XML document examples in this tutorial!
An External JavaScript for loadXMLDoc()
To make the code above even easier to maintain, and to make sure the same
code is used in all pages, we store the function in an external file.
The file is called "loadxmldoc.js", and will be loaded in the head section of an HTML page.
Then, the loadXMLDoc()
function can be called from a script in the page.
The following example uses the loadXMLDoc() function to load books.xml:
Example
<html>
<head>
<script type="text/javascript" src="loadxmldoc.js">
</script>
</head>
<body>
<script type="text/javascript">
xmlDoc=loadXMLDoc("books.xml");
code goes here.....
</script>
</body>
</html> |
|
How to get the data from the XML file, will be explained in the next
chapters.
The loadXMLString() Function
To make the code
from the previous page simpler to maintain (and check for older browsers), it should be written as a function:
function loadXMLString(txt)
{
try //Internet Explorer
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";
xmlDoc.loadXML(txt);
return xmlDoc;
}
catch(e)
{
try //Firefox, Mozilla, Opera, etc.
{
parser=new DOMParser();
xmlDoc=parser.parseFromString(txt,"text/xml");
return xmlDoc;
}
catch(e) {alert(e.message)}
}
return null;
} |
The function above can be stored in the <head> section of an HTML page, and called from a script in the page.
The function described above, is used in all
XML string examples in this tutorial!
An External JavaScript for loadXMLString()
We have stored the loadXMLString() function in a file called "loadxmlstring.js".
Example
<html>
<head>
<script type="text/javascript" src="loadxmlstring.js"></script>
</head>
<body>
<script type="text/javascript">
text="<bookstore>"
text=text+"<book>";
text=text+"<title>Everyday Italian</title>";
text=text+"<author>Giada De Laurentiis</author>";
text=text+"<year>2005</year>";
text=text+"</book>";
text=text+"</bookstore>";
xmlDoc=loadXMLString(text);
code goes here.....
</script>
</body>
</html> |
|

Whether you're new to XML or already an advanced user,
the user-friendly views and powerful entry helpers,
wizards, and debuggers in XMLSpy are designed to meet your XML
and Web development needs from start to finish.
- XML editor
- Graphical XML Schema / DTD editors
- XSLT 1.0/2.0 editor, debugger, profiler
- XQuery editor, debugger, profiler
- XBRL validator & taxonomy editor
- Support for Office Open XML (OOXML)
- Graphical WSDL editor & SOAP debugger
- Java, C#, C++ code generation
- And much more!
Download a free trial today!
|
|
|
|