trouble using mozilla xml loading code

M

mr_burns

Hi there,

I am using the following function to import a xml file whether the
users browser be IE or Mozilla:

function importXML(file) {
var xmlDoc;
var moz = (typeof document.implementation != 'undefined') && (typeof
document.implementation.createDocument != 'undefined');
var ie = (typeof window.ActiveXObject != 'undefined');

if (moz) {
xmlDoc = document.implementation.createDocument("", "", null)
//do I need something else here???
} else if (ie) {
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
while(xmlDoc.readyState != 4) {};
}
xmlDoc.load(file);
return xmlDoc
}



An example call of this function looks like this:

importXML('xml/books.xml')

The code for IE works well as I have used code to access the XML file
im using but the code for Mozilla doesnt work. I got the funxtion from
a tutorial site, http://www.sitepoint.com/article/xml-javascript-mozilla.

Am I missing something after the line 'xmlDoc =
document.implementation.cre...?? On the tutorial it had a line
xmlDoc.onload = readXML but that appears to be some kind of function,
readXML, and isnt shown on the tutorial. How do I get the variable
xmlDoc to the same stage as the code used for IE so that I can then
perform something like after the function is run??:

nodes = xmlDoc.getElementsByTagName("title")

or

nodes = xmlDoc.documentElement.childNodes


If I try the above in Mozilla and do something like nodes.length I get
0 (wrong) whereas if I do it in IE, I get 10 (correct). Is the 'nodes
= ...' code only able to run in IE or is the variable, xmlDoc, not
properly prepared in Mozilla to do the above?? The results I get in
Mozilla lead me to assume either but I may be wrong. Any help would be
great.

Cheers

Burnsy
 
M

Martin Honnen

mr_burns wrote:

I am using the following function to import a xml file whether the
users browser be IE or Mozilla:

function importXML(file) {
var xmlDoc;
var moz = (typeof document.implementation != 'undefined') && (typeof
document.implementation.createDocument != 'undefined');

Other browsers have document.implementation.createDocument as well so at
least the name of the variable 'moz' is doubtful.

While Mozilla allows you to create an XML document with the method you
check for above and then exposes a load method that stuff is Mozilla
only while by now Safari and recent Opera version implement
XMLHttpRequest thus I would strongly suggest to use that if you want to
load XML:

function loadXML (url, processXML) {
var httpRequest;
if (typeof XMLHttpRequest != 'undefined') {
httpRequest = new XMLHttpRequest();
}
else if (typeof ActiveXObject != 'undefined') {
httpRequest = new ActiveXObject('Microsoft.XMLHTTP');
}
if (httpRequest) {
httpRequest.open('GET', url, true);
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4 && httpRequest.responseXML) {
processXML(httpRequest.responseXML);
}
};
httpRequest.send(null);
}
}

function exampleXMLHandler (xmlDocument) {
if (xmlDocument.documentElement) {
alert(xmlDocument.documentElement.childNodes.length);
}
}


loadXML('test2005010601.xml', exampleXMLHandler)

That should do with IE/Win (versions 5, 5.5, 6 and hopefully later),
Mozilla 1.0 and later, Safari 1.2 (not tested now), Opera 7.6x , 8.00 or
later, as long as the XML loaded is well-formed. If the parsing of the
XML fails then unfortunately every implementation has its own way to
indicate parse errors so catering for that is a bit too much to handle
here in a newsgroup post.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top