【发布时间】: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