gpt4 book ai didi

javascript - 将 XML 加载到 DOM 中并替换 Microsoft.XMLDOM 的用法

转载 作者:行者123 更新时间:2023-11-30 05:31:01 25 4
gpt4 key购买 nike

我正在使用一些遗留代码,在很多地方都有代码可以从某个 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"); 时定义的;

对于其他浏览器以及 IE-11,DOMParser 是建议的方法。

这就是我当时从 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);
}

最佳答案

您可以采取以下步骤:

  1. 转到 Internet 选项。
  2. 选择安全选项卡。
  3. 在自定义级别下。
  4. 确保Initialize and script active x controls is not marked safe for scripting选择启用的单选按钮并立即尝试运行您的代码。

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

关于javascript - 将 XML 加载到 DOM 中并替换 Microsoft.XMLDOM 的用法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27071949/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com