How do you run an xPath query in IE11?

前端 未结 2 804
说谎
说谎 2020-12-15 08:18

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

Prior to IE 11, IE supported using xmldoc.select

2条回答
  •  一向
    一向 (楼主)
    2020-12-15 08:39

    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 }
    

    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!)

    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!

    Thanks all for your help.

提交回复
热议问题