Manipulating a XML file using DHTML
In this tutorial, we look at using DHTML to retrieve and display a XML file. This is useful when you need a client side method of including XML files on your webpages.
Retrieving a XML file using DHTML
First and foremost, I should mention that XML file retrieval is only possible in IE5+/NS6+, using their respective DOMs (Document Object Model). The technique required is different for the two browsers (yes, a big surprise!).
- Retrieving a XML file in IE5+
In IE5+, we rely on the ActiveXObject() object to fetch a XML file:
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.load("myfile.xml");
With the above, IE5 loads "myfile.xml" into browser memory, awaiting further instructions from you.
- Retrieving a XML file in NS6+
In NS6, the syntax required is equally straightforward, though one may add, unorthodox:
var xmlDoc= document.implementation.createDocument("","doc",null); xmlDoc.load("myfile.xml");
- Cross browser XML file retrieval
An unavoidable step these days, let's mash everything together and derive at a cross browser code:
if (window.ActiveXObject) var xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); else if (document.implementation && document.implementation.createDocument) var xmlDoc= document.implementation.createDocument("","doc",null); xmlDoc.load("myfile.xml");
Notice how two levels of object detection is required in NS6's case. There you have it- XML file retrieval the DHTML way!