Forum Moderators: open

Message Too Old, No Replies

reading XML with client-side JavaScript

Which browsers can do this?

         

Purple Martin

11:22 pm on Feb 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've had an idea that would work great if I could read an xml file using client-side JavaScript. I've found an example that is IE specific, but apparently not all browsers support this sort of thing.

What browsers can do this?

DrDoc

11:30 pm on Feb 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you expand on that a little? :)
How does it work?

Purple Martin

11:43 pm on Feb 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, for example it would be a great way to read in a multi-level heirarchical menu (or a nav tree).

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

DrDoc

11:50 pm on Feb 9, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What does the Mozilla way of doing it look like? I have never dealt with this sort of stuff (in JS). But, you would think that the DOM leaves room for it...

Purple Martin

12:08 am on Feb 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Mozilla looks like this (I found these examples with Google):

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?

DrDoc

8:29 pm on Feb 10, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Build a test document and use browsercam? :)

Purple Martin

5:44 am on Feb 11, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Browsercam rocks!

But the test results were disappointing. My page successfully read an XML file in IE5.0 and up (PCs only), and also in AOL7, but that was it.

It didn't work for Mozilla/Netscape/Opera/others. Even though I've got code that's supposed to be just for them (as posted above).

:'-(

Purple Martin

3:25 am on Feb 12, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes! I got it working in Mozilla/Netscape! There was a simple bug in the tutorial's code.

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