如何在IE11中运行xPath查询? [英] How do you run an xPath query in IE11?

查看:935
本文介绍了如何在IE11中运行xPath查询?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在系统中的某个时刻,我们使用javascript读取了一大块XML,然后使用xPath查询该XML文档.

At one point in our system we use javascript to read in a chunk of XML and then query that XML document using xPath.

在IE 11之前,使用xmldoc.selectSingleNode("//xpath/string")支持IE,并使用xmldoc.evaluate("//xpath/string")支持非IE浏览器.这些都返回了一个相似的对象,然后我们可以进行解释以提取所需的数据.

Prior to IE 11, IE supported using xmldoc.selectSingleNode("//xpath/string") and the non IE browsers supported using a xmldoc.evaluate("//xpath/string"). These both returned a similar object that we could then carry on interpreting to extract the data required.

在IE11中,这些方法似乎都不可用.

In IE11 neither of these methods seem to be available.

似乎IE11对XML文档提供了一些支持,因为当我使用parseFromString方法使用DOMParser对象读取xml时,它将返回一个对象,IE11调试器将调用该对象作为XMLDocument.

It seems that IE11 has some support for XML documents in that when I read in the xml using the DOMParser object using the parseFromString method, it returns an object that the IE11 debugger calls an XMLDocument.

推荐答案

感谢@Martin Honnen指出IE11仍支持某些ActivXObject!

Thanks to @Martin Honnen for pointing out that some ActivXObjects are still supported in IE11!

var doc;
try { 
    doc = new ActiveXObject('Microsoft.XMLDOM'); 
    doc.loadXML(stringVarWithXml); 
    var node = doc.selectSingleNode('//foo'); 
} catch (e) { // deal with case that ActiveXObject is not supported }

我使用了"Microsoft.XMLDOM",因为它已被显示为

I've used "Microsoft.XMLDOM" as it is sugested here that it is a more generic call to what ever xml parser is present on the system, where as it sounds like "Msxml2.DOMDocument.6.0" will fail if that exact version is not present. (We do have to support all IE vers back to 6.0 at my place!)

这就像往常一样工作.我唯一的问题是,我用来检测IE与其他浏览器的旧开关if (typeof ActiveXObject !== "undefined")失败了,因为我想他们正试图阻止其使用!

This just works as it always has done. The only problem I had was that the old switch I used to detect IE vs other browsers was if (typeof ActiveXObject !== "undefined") failed as I guess they are trying to discourage it's use!

感谢您的帮助.

这篇关于如何在IE11中运行xPath查询?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆