w3schools
Search W3Schools :  
  
HOME HTML CSS XML JAVASCRIPT ASP PHP SQL MORE...   References Examples Forum About

XML DOM Load Functions

« Previous Next Chapter »

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)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}

The function above can be stored in the <head> section of an HTML page, and called from a script in the page.

lamp 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>

Try it yourself »

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)
{
if (window.DOMParser)
  {
  parser=new DOMParser();
  xmlDoc=parser.parseFromString(txt,"text/xml");
  }
else // Internet Explorer
  {
  xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async="false";
  xmlDoc.loadXML(txt);
  }
return xmlDoc;
}

The function above can be stored in the <head> section of an HTML page, and called from a script in the page.

lamp 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>

Try it yourself »

« Previous Next Chapter »


Altova® MapForce®
Graphical XML Mapping Tool from the Developers of XMLSpy®

Altova MapForce

Need an easy way to get data into XML, or transform XML to another format? MapForce lets you map XML data to/from any combination of XML, database, flat file, Excel 2007, XBRL, or Web services data. Then it transforms data instantly or auto-generates royalty-free data integration code for recurrent conversions.

Download a free, fully functional 30-day trial to experience the following features:
  • Easy-to-use, graphical data mapping interface
  • Instant data transformation
  • XSLT 1.0/2.0 and XQuery code generation
  • Java, C#, and C++ code generation
  • Advanced data processing functions
  • Support for all major relational databases including SQL Server, IBM DB2, Oracle, and more
  • Visual Studio & Eclipse integration

Download a fully-functional trial today!

  Altova MapForce


WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Top 10 Web Hosting
UK Reseller Hosting
Web Hosting
FREE Web Hosting
Top Web Hosting
Windows Hosting
WEB BUILDING
Download XML editor
FREE Flash Website
FREE Web Templates
Website Monetization
FLIGHT TICKETS
Find the cheapest flight
to any destination now!
EDUCATION
US Web Design Schools
HTML Certification
JavaScript Certification
XML Certification
PHP Certification
ASP Certification
STATISTICS
Browser Statistics
Browser OS
Browser Display
W3Schools.com HOME | TOP | PRINT | FORUM | ABOUT
W3Schools is for training only. We do not warrant the correctness of its content. The risk from using it lies entirely with the user.
While using this site, you agree to have read and accepted our terms of use and privacy policy.
Copyright 1999-2009 by Refsnes Data. All Rights Reserved.