Forum Moderators: open
This function will read the XML file into a JavaScript object (in IE, there is a different way to do it in Mozilla):
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
function loadXML(xmlFile)
{
xmlDoc.async="false";
xmlDoc.onreadystatechange=verify;
xmlDoc.load(xmlFile);
xmlObj=xmlDoc.documentElement;
}
Then you can access the XML information really easily, for example:
document.write(xmlObj.childNodes(1).getAttribute("stuff"))
It wouldn't be too hard to write a recursive loop to run through all the levels of children and build the menu/tree/blog/whatever. Once I've got the basic loop done, I could reuse it for many projects :-D
var xmlDoc;
function importXML(file) {
xmlDoc=document.implementation.createDocument("", "doc", null)
xmlDoc.load(file);
xmlDoc.onload = readXML;
}
I'm just wondering how many versions back these will work in IE and Mozilla, and if it's possible in any other browsers. Does anyone know?
The following code will read XML into JavaScript in IE5+ (except Mac), Mozilla and Netscape 6+:
var ie = (typeof window.ActiveXObject!= 'undefined');
var moz = (typeof document.implementation!= 'undefined') &&
(typeof document.implementation.createDocument!= 'undefined');
var xmlDoc;
function importXML(file) {
if (moz) {
xmlDoc = document.implementation.createDocument("", "doc", null) ;
xmlDoc.onload = readXML;
xmlDoc.load(file);
} else if (ie) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
while(xmlDoc.readyState!= 4) {};
xmlDoc.load(file);
readXML();
}
}importXML("myFileName.xml");
function readXML() {
root = xmlDoc.documentElement;
// now do whatever you like with it!
}
In case you're interested, here is the full list of what works and what doesn't:
Works
AOL 7.0 - Win 2000
Explorer 5.0 - Win 2000
Explorer 5.5 - Win 2000
Explorer 6.0 - Win 2000
Explorer 6.0 - Win XP
Mozilla 1.5 - Win 2000
Mozilla 1.6 - Linux
Mozilla 1.6 - Macintosh
Mozilla 1.6 - Win XP
Netscape 6.2 - Win 2000
Netscape 7.0 - Linux
Netscape 7.0 - Macintosh
Netscape 7.0 - Win 2000
Netscape 7.1 - Win XP
Doesn't
Konqueror 3.0.5 - Linux
Explorer 4.0 - Win 98 (gives error)
Explorer 5.2 - Macintosh
Netscape 4.78 - Win 2000
Netscape 4.8 - Linux
Opera 6.0 - Macintosh
Opera 7 - Win 2000
Opera 7 - Win XP
Safari 1.0 - Macintosh