XML DOM Load Function

## XML DOM Tutorial

DOM HOME DOM Introduction DOM Nodes DOM Node Tree DOM Parser DOM Load Function DOM Methods DOM Accessing DOM Node Info DOM Node List DOM Traversing DOM Browsers DOM Navigating

## Manipulate Nodes

DOM Get Values DOM Change Nodes DOM Remove Nodes DOM Replace Nodes DOM Create Nodes DOM Add Nodes DOM Clone Nodes DOM XMLHttpRequest

## XML DOM Reference

DOM Node Types DOM Node DOM NodeList DOM NamedNodeMap DOM Document DOM DocumentImpl DOM DocumentType DOM ProcessingInstr DOM Element DOM Attribute DOM Text DOM CDATA DOM Comment DOM XMLHttpRequest DOM ParseError Obj DOM Parser Errors
DOM Summary

## XML DOM Examples

DOM Examples DOM Validator

# 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.

| 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 src="loadxmldoc.js">
</script>
</head>
<body>
<script>
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.

| 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 src="loadxmlstring.js"></script>
</head>
<body>
<script>
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 »

## Your suggestion:

Your E-mail (optional):

Page address:

Description:

Close [X]

## Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

## WEB HOSTING

Best Web Hosting UK Reseller Hosting Wow! $5 SSD Hosting

## WEB BUILDING

Download XML Editor
FREE Website BUILDER FREE Website Creator

## WEB RESOURCES

Web Statistics Web Validation

## SHARE THIS PAGE