Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you run an xPath query in IE11?

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.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.

In IE11 neither of these methods seem to be available.

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.

like image 821
pixelmatt Avatar asked Oct 10 '13 16:10

pixelmatt


1 Answers

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.

like image 155
pixelmatt Avatar answered Oct 31 '22 17:10

pixelmatt