ActiveXObject not working in IE

Hi all

I am currently trying to create an XML document, in IE, like so


  xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
  xmlDoc.async = 'false';
  xmlDoc.loadXML(myXmlString);

But its not working.
If I alert the value for xmlDoc, its nothing.
I looked around the net and found this code on the w3 schools site

http://www.w3schools.com/Dom/tryit.asp?filename=try_dom_parser2

and it works fine.
If I put an alert after the first like, it displays “object”.

Does anyone have any suggestions for me?
Thanks
:slight_smile:

Have a look at the code below, working for me in both IE7, 8 and Firefox 2, 3.



function(namespaceURI,  documentType){
	var xmlDocument = null;
	namespaceURI = namespaceURI || '';
	documentType = documentType || null;
	if (document.implementation && document.implementation.createDocument) {
		xmlDocument = document.implementation.createDocument(
			"",
			"",
			documentType
		);
	}
	else if (typeof ActiveXObject != 'undefined') {
		try {
			xmlDocument = new ActiveXObject('Microsoft.XMLDOM');
		}
		catch (e) {
		}
	}
}



Cool thanks for your code, ill have a look at it.
I did how ever figure out my “problem”
After creating my xmlDoc variable
I alerted it to check if it was there and was an object, like so


  alert('place 1 - ' + xmlDoc);

For some reason (only in IE…) if the xmlDoc variable isn’t the only thing in the alert, it displays nothing. So when I tried


  alert(xmlDoc);

It displayed “[object]”

Very very very strange
Can anyone shed some light?

When you are loading an XML file using ActiveXObject in IE, You have to look for the readystate as in the case of an Ajax request.

In my case (well my current case), i’m not actually using a file, im inputting a string. So would I still have to check for the readystate?

Code below is working for me…


var txt = '<Person><Name>ArunB</Name><Age>22</Age></Person>';
if(document.all){
	var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
	xmlDoc.async="false";
	xmlDoc.loadXML(txt);
}
else{
	var parser=new DOMParser();
	var xmlDoc=parser.parseFromString(txt,"text/xml");
}

$("Person", xmlDoc).each(function(){
	alert($(this).find("Name").text());
});

I don’t think you getting exactly what im saying.
My code is working, but the messages that you get when alerting you XML object are strange.

Run this code


    var txt = '<Person><Name>ArunB</Name><Age>22</Age></Person>';
    if(document.all) {
      var xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
      xmlDoc.async="false";
      xmlDoc.loadXML(txt);
    } else {
      var parser=new DOMParser();
      var xmlDoc=parser.parseFromString(txt,"text/xml");
    }
 
    alert(xmlDoc);
    alert('1 - ' + xmlDoc);

Check the results you get from the alerts (in IE). Its really strange…

Just on another note. I tried running your code, and got an error
“$ is not defined”
I haven’t seen that syntax before, what exactly is $?

$ is jQuery.


I haven’t yet come upon such behavior in IE, it is quite strange really.

alert((xmlDoc + "") == undefined);

This will alert true. :nono:

alert(xmlDoc == undefined);

This will alert false. :confused: