DOM loading function
XML DOM Loading function
Loading code in an XML document can be stored in a function.
loadXMLDoc() function
To make the code in the previous page easier to maintain (check for older browsers), it should be written as a function:
{
if (window.XMLHttpRequest)
{
xhttp=new XMLHttpRequest();
}
else
{
xhttp =new ActiveXObject("Microsoft.XMLHTTP");
}
xhttp.open("GET",dname,false);
xhttp.send();
return xhttp.responseXML;
}
The above function can be stored in the <head> section of the HTML page and called from a script in the page.
The functions described above are used for all XML document instances in this tutorial!
External JavaScript for loadXMLDoc()
To make the above code easier to maintain and ensure that the same code is used in all pages, we store the function in an external in the file.
The file name is "loadxmldoc.js" and is loaded in the head section of the HTML page. The script in the page then calls the loadXMLDoc() function.
The following example uses the loadXMLDoc() function to load books.xml:
Example
<!DOCTYPE html> <html> <head> <script src="loadxmldoc.js"></script> </head> <body> <script> xmlDoc=loadXMLDoc("books.xml"); document.write(xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue + "<br>"); document.write(xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue + "<br>"); document.write(xmlDoc.getElementsByTagName("year")[0].childNodes[0].nodeValue); </script> </body> </html>
Running Example»
Click the "Run Example" button to view the online example
How to obtain data from the XML file will be explained in the next chapter.
loadXMLString() function
To make the code in the previous page easier to maintain (check for older browsers), it should be written as a function:
{
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 above function can be stored in the <head> section of the HTML page and called from a script in the page.
The functions described above are used for all XML string instances in this tutorial!
External JavaScript for loadXMLString()
We have stored the loadXMLString() function in a file called "loadxmlstring.js".
Instance
<!DOCTYPE html> <html> <head> <script src="loadxmlstring.js"></script> </head> <body> <script> text="<bookstore><book>"; text=text+"<title>Everyday Italian</title>"; text=text+"<author>Giada De Laurentiis</author>"; text=text+"<year>2005</year>"; text=text+"</book></bookstore>"; xmlDoc=loadXMLString(text); document.write(xmlDoc.getElementsByTagName("title")[0].childNodes[0].nodeValue); document.write("<br>"); document.write(xmlDoc.getElementsByTagName("author")[0].childNodes[0].nodeValue); document.write("<br>"); document.write(xmlDoc.getElementsByTagName("year")[0].childNodes[0].nodeValue); </script> </body> </html>
Run Instance»
Click the "Run Instance" button to view the online instance