crackhaus
crackhaus

Reputation: 1196

Loading XML into the DOM and replacing usage of Microsoft.XMLDOM

I'm working with a some legacy code and in many places there's code to get XML data from some url. it's pretty straight forward.

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async="false";           
xmlDoc.load(url);

and in some places

var httpRequest= new ActiveXObject("Msxml2.XMLHTTP.6.0");
httpRequest.open('GET', url, false);
httpRequest.send(null);

in most cases we're picking results from response XML. I've seen in a number of places that the usage of "Microsoft.XMLDOM" is antiquated, replacing it in favor of ActiveXObject("Msxml2.XMLHTTP.6.0"). For other browsers I should use the standard W3C XMLHttpRequest. This seems to work without any problems.

The problem is loading the result xml string. I see that loadXML is defined when using "Microsoft.XMLDOM" but not with ActiveXObject("Microsoft.XMLHTTP");

With other browsers DOMParser is the suggested method as well as IE-11.

This is what I did to retrieve information from a url then parse that information and then finally attempt to load the XML string to the DOM. My main problem is I'm getting more and more confused as to what solution is appropriate when Manipulating XML with regards to Internet Explorer or maybe it's just too late in the day. I wanted to remove the usage of "Microsoft.XMLDOM" but to perform the loadXML I had to go back to it. is there a better way to approach this?

 // Get the information use either the XMLHttpRequest or ActiveXObject
 if (window.ActiveXObject || 'ActiveXObject' in window) {
        httpRequest = new ActiveXObject("Msxml2.XMLHTTP.6.0");
        }
        else if (window.XMLHttpRequest) {
            httpRequest = new XMLHttpRequest();
            if (httpRequest.overrideMimeType) {
                httpRequest.overrideMimeType('text/xml');
    }
}

httpRequest.open('GET', url, false);
httpRequest.send();

var xmlDoc = httpRequest.responseXML;

// Retrieve the XML into the DOM
var xml = xmlDoc.getElementsByTagName("XML_STRING_SETTINGS")


// Load the XML string into the DOM
if (window.DOMParser) {
    var parser = new DOMParser();
    xmlDoc = parser.parseFromString(xml, "text/xml");
}
else // code for IE
{
    xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
    xmlDoc.async = false;
    // is there another way to do this load? 
    xmlDoc.loadXML(xml);
}

Upvotes: 3

Views: 10584

Answers (2)

Ramesh Yamalapalli
Ramesh Yamalapalli

Reputation: 11

You may take following steps:

  1. Go to internet options.
  2. Select security tab.
  3. Under Custom level.
  4. Ensure that Initialize and script active x controls is not marked safe for scripting select the radio button enabled and try to run your code now.

I am sure your problem will be solved.

Upvotes: 1

Verhaeren
Verhaeren

Reputation: 1661

I think the code for IE5 and 6 is

new ActiveXObject("Microsoft.XMLHTTP")

And for IE7+ (and the rest of browsers)

new XMLHttpRequest();

Upvotes: 0

Related Questions