TypeError: The value undefined (the result of the xmlDoc.load expression) is not an object

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 download function that I am currently using mainly consists of w3schools tutorials:

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

The exact code looks like this:

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 = url of the xml file. This code gets "TypeError: Value undefined (the result of the xmlDoc.load expression) is not an object." in Safari.

I also tried the code on this site:

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

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

+4
source share
2 answers

It seems that the problem is that Safari does not support document.implementation.createDocument as a method for extracting and loading XML sources. You must use XMLHttpRequest to retrieve and parse AFAIK XML.

I tried a modified version of the code from the Apple tutorial that you linked, and it seems to work for me. This code is not the best in the world, and it misses a lot of processing errors, but this is the only evidence I had at hand.

Note. I highly recommend using the library. There are browser incompatibilities with XMLHttpRequests and XML syntax. It is worth the investment!

For the non-library version, I used a modified version of safari code to get 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(""); } } 

Capturing XML from the result is not without its own entry:

 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 have problems, there are a few things you can verify that are most likely to solve your problem.

  • Have you set your content type to text / xml?
  • Is your request actually sending to and from the server?
  • When you warn / check responseText, do you see something that does not belong?
  • Is your XML formatted correctly? Run it through the validator.

Good luck Greetings.

+1
source

You might want to see the XML for <Script> . I saw several posts that indicate that they solved the problem with Safari with it.

0
source

All Articles