最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Loading XML into the DOM and replacing usage of Microsoft.XMLDOM - Stack Overflow

programmeradmin0浏览0评论

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);
}

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);
}
Share Improve this question edited Nov 22, 2014 at 6:45 crackhaus asked Nov 21, 2014 at 23:14 crackhauscrackhaus 1,1962 gold badges15 silver badges27 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 1

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.

I think the code for IE5 and 6 is

new ActiveXObject("Microsoft.XMLHTTP")

And for IE7+ (and the rest of browsers)

new XMLHttpRequest();
发布评论

评论列表(0)

  1. 暂无评论