views:

2821

answers:

2

I am trying to load an XML file using Javascript and I have yet to find a good function that works in IE, Firefox, and Safari. The load function I am currently using is basically the one straight out of the w3schools tutorials:

http://www.w3schools.com/XML/tryit.asp?filename=tryxml_dom_createelement

The exact code looks like:

if (window.ActiveXObject)
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation.createDocument)
{
xmlDoc=document.implementation.createDocument("","",null);
}
else
{
alert('Your browser cannot handle this script');
}
xmlDoc.async=false;
xmlDoc.load(dname);

Where dname = the url of the xml file. This code gets a "TypeError: Value undefined (result of expression xmlDoc.load) is not object." in Safari.

I have also tried the code on this site:

http://developer.apple.com/internet/webcontent/xmlhttpreq.html

However, it yields a null XML file. Can anyone help?

A: 

You might want to look at XML for <Script>. I've seen some posts that indicate that they've solved the problem on Safari with it.

tvanfosson
Thanks for the quick reply! That library work on the browsers I need. But I would have to rewrite all my code to use that library. Ideally I would just like to update my load xml function to work in Safari, but if there is a load xml function I can import from a library that would work too.
I got the same problem. Any other ideas besides using a third party library?
dev.e.loper
+1  A: 

Sounds like the problem is that Safari does not support document.implementation.createDocument as a method to fetch and load XML sources. You must use an XMLHttpRequest to fetch and parse the XML AFAIK.

I've tried a modified version of the code from the Apple tutorial you linked and it seemed to work for me. This code is not the best in the world, and it's missing a lot of error handling, but it's the only proof of concept I had on hand.

Note: I highly recommend using a library. There are browser inconsistencies abound with XMLHttpRequests and XML parsing. It's worth the investment!

For a non library version I used a modified version of the safari code to get the XMLHttpRequest:

function getXHR(url,callback) {
 var req = false;
 // branch for native XMLHttpRequest object
 if(window.XMLHttpRequest && !(window.ActiveXObject)) {
  try {
   req = new XMLHttpRequest();
  } catch(e) {
   req = false;
  }
 // branch for IE/Windows ActiveX version
 } else if(window.ActiveXObject) {
  try {
   req = new ActiveXObject("Msxml2.XMLHTTP");
  } catch(e) {
   try {
    req = new ActiveXObject("Microsoft.XMLHTTP");
   } catch(e) {
    req = false;
   }
  }
 }

 if(req) {
  req.onreadystatechange = function() { callback( req ) };
  req.open("GET", url, true);
  req.send("");
 }
}

Grabbing the XML from the result is not without its own quirks as well:

function getXML( response ) {
 if( response.readyState==4 ) {
  //Get the xml document element for IE or firefox
  var xml;
  if ( response.responseXML ) {
    xml = new ActiveXObject("Microsoft.XMLDOM");
    xml.async = false;
    xml.loadXML(response.responseText);
  } else {
    xml = response.responseXML;
  }

  return xml;
 }

 return null;
}

Finally use what you get:

function callback( response ) {
 var xmlDoc = getXML( response );
 if( xmlDoc ) {
  //do your work here
  ...
 }  
}

If you still find yourself having trouble there are a few things you can check that will likely solve your problem.

  1. Did you set your content type to text/xml?
  2. Is your request actually making it to the server and back?
  3. When you alert/examine the responseText, do you see anything that does not belong?
  4. Is your XML properly formatted? Run it through a validator.

Best of luck! Cheers.

coderjoe