By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,607 Members | 1,819 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,607 IT Pros & Developers. It's quick & easy.

trouble using mozilla xml loading code

P: n/a
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
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a


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.lengt h);
}
}
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.