How to get the browsers to find and read XML files




Reading XML files

Creating an XML file is one thing, retrieving the content is something else. Extraction of data from an XML file takes place in two steps:
  1. Getting access and reading the database (shown on this page)
  2. Reading/searching in the accessed tile (shown on the next pages)
Accessing an XML file is one of the instances where the underlying browser technologies make a difference. Internet Explorer uses something called ActiveXObject while the other browsers use XMLHttpRequest, so here you start by asking, not for browser type as could be expected, but whether the browser uses ActiveXObject or XMLHttpRequest. The entire code looks like this:

function loadXMLDoc(dname)
{
var xmlDoc;
if (window.ActiveXObject) {
     xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
     xmlDoc.async = "false";
     }
else if (window.XMLHttpRequest) {
     xmlDoc=new window.XMLHttpRequest();
     xmlDoc.open("GET",dname,false);
     xmlDoc.send("");
     return xmlDoc.responseXML.documentElement;
     }
else {
     alert('Your browser cannot run the script');
     }
xmlDoc.async=false;
xmlDoc.load(dname);
return(xmlDoc);
}

The way the code was written here, it was made to be called from a .js file. I always have it saved as loadXMLDoc.js in a directory called JavaScripts. On the pages where I need to access an XML file, I have the following line in the HEAD tag, reading the script:

<SCRIPT TYPE="text/javascript" SRC="JavaScripts/LoadXMLDoc.js">

The script is hereby loaded along with the other meta data, when the page is accessed, and can now be executed when needed, and it will read the XML file you specify. This approach is mostly useful when you have a site accessing several XML files. If you only have one file to read, you can write the file name directly, instead of using the variable dname. For the XML file MyXML-file.xml the code will now look like this:

function loadXMLDoc()
{
var xmlDoc;
if (window.ActiveXObject) {
     xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
     xmlDoc.async = "false";
     }
else if (window.XMLHttpRequest) {
     xmlDoc=new window.XMLHttpRequest();
     xmlDoc.open("GET","MinXML-fil.xml",false);
     xmlDoc.send("");
     return xmlDoc.responseXML.documentElement;
     }
else {
     alert('Your browser cannot run the script');
     }
xmlDoc.async=false;
xmlDoc.load("MyXML-file.xml");
return(xmlDoc);
}

NOTE! Several browsers can't access and read XML files, unless placed on a web server. If you test the script while placed on a local hard drive or a network drive it will work just fine with Firefox, for Internet Explorer it will work with some versions and in Chrome and Safari it won't work at all. This is not a programming error, but a limitation in the technology that makes the browser work the way they do.

Alternatives to XMLHttpRequest: On occasion you see an alternative to XMLHttpRequest for reading XML files. It looks like this:

if (document.implementation && document.implementation.createDocument) {
     xmlDoc = document.implementation.createDocument("", "", null);
     xmlDoc.load(dname);
     }

In my experience, it doesn't work for all browsers, so I recommend using XMLHttpRequest.