【问题标题】:Loading XML into the DOM and replacing usage of Microsoft.XMLDOM将 XML 加载到 DOM 并替换 Microsoft.XMLDOM 的使用
【发布时间】:2014-11-21 23:14:27
【问题描述】:

我正在使用一些遗留代码,并且在许多地方都有从某个 url 获取 XML 数据的代码。这很简单。

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

在某些地方

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

在大多数情况下,我们从响应 XML 中挑选结果。 我在很多地方看到“Microsoft.XMLDOM”的使用已经过时,取而代之的是 ActiveXObject(“Msxml2.XMLHTTP.6.0”)。 对于其他浏览器,我应该使用标准的 W3C XMLHttpRequest。这似乎没有任何问题。

问题在于加载结果 xml 字符串。 我看到 loadXML 是在使用“Microsoft.XMLDOM”时定义的,但不是在 ActiveXObject("Microsoft.XMLHTTP");

对于其他浏览器,建议使用 DOMParser 以及 IE-11。

这就是我从 url 中检索信息所做的事情 解析该信息,然后最终尝试将 XML 字符串加载到 DOM。 我的主要问题是,在针对 Internet Explorer 操作 XML 时,我越来越困惑什么解决方案是合适的,或者可能为时已晚。 我想删除“Microsoft.XMLDOM”的使用,但要执行 loadXML,我必须回到它。有没有更好的方法来解决这个问题?

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

【问题讨论】:

    标签: javascript xml internet-explorer-11


    【解决方案1】:

    您可以采取以下步骤:

    1. 转到互联网选项。
    2. 选择安全选项卡。
    3. 在自定义级别下。
    4. 确保 Initialize and script active x 控件未标记为可安全执行脚本选择已启用的单选按钮并立即尝试运行您的代码。

    我相信你的问题会得到解决的。

    【讨论】:

      【解决方案2】:

      我认为IE5和6的代码是

      new ActiveXObject("Microsoft.XMLHTTP")
      

      对于 IE7+(以及其他浏览器)

      new XMLHttpRequest();
      

      【讨论】:

        猜你喜欢
        • 2016-07-25
        • 2012-06-07
        • 2015-08-18
        • 1970-01-01
        • 2011-09-24
        • 1970-01-01
        • 1970-01-01
        • 2012-09-09
        • 1970-01-01
        相关资源
        最近更新 更多