14

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.

pixelmatt
  • 649
  • 2
  • 8
  • 19
  • Does the `XMLDocument` maybe contain the method `querySelector`? – Niet the Dark Absol Oct 10 '13 at 16:08
  • Yes it does but that doesn't seem to support an xpath selector like "//xpath/selector" – pixelmatt Oct 10 '13 at 16:10
  • Didn't IE10 yank support for it? – epascarello Oct 10 '13 at 16:14
  • @epascarello [yes, it seems to be](http://stackoverflow.com/questions/13521554/xpath-in-internet-explorer-10-gone) – Pointy Oct 10 '13 at 16:15
  • Well I've read a lot about IE10 removing support, but that's not my experience, you had to use a combination of the new world DOMParser and then you could use the old selectSingleNode for the query. IE10 removed support for the old ActiveXObject method used to read in XML. – pixelmatt Oct 10 '13 at 16:18
  • Also just had a go with [Wicked Good xPath](http://code.google.com/p/wicked-good-xpath/) this doesn't seem to work fully with IE11, it seems to fail to produce an object with anything in it. – pixelmatt Oct 11 '13 at 08:55
  • `selectSingleNode` is a method supported by the MSXML DOM. According to http://msdn.microsoft.com/en-us/library/ff955298%28v=vs.85%29.aspx, the Javascript engine in IE 11 continues to support e.g. `new ActiveXObject('Msxml2.DOMDocument.6.0')` to create an MSXML DOM document. I don't have IE 11 to test, what happens if you do `var doc; try { doc = new ActiveXObject('Msxml2.DOMDocument.6.0'); doc.loadXML(stringVarWithXml); var node = doc.selectSingleNode('//foo'); } catch (e) { // deal with case that ActiveXObject is not supported }`? – Martin Honnen Oct 11 '13 at 10:09
  • Sounds interesting, I will give it ago, I thought IE had given up on ActiveX since IE10. – pixelmatt Oct 11 '13 at 14:03
  • Also note that you can specify `` in your documents to enforce compatibility with IE 10. – Martin Honnen Oct 11 '13 at 15:01

2 Answers2

6

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.

pixelmatt
  • 649
  • 2
  • 8
  • 19
  • 2
    `Msxml2.DOMDocument.6.0` is available on all supported Microsoft OS, even Windows XP with the latest service pack. `Microsoft.XMLDOM` is more generic but these days defaults to MSXML 3 and that version with `selectNodes` and `selectSingleNode` does not support XPath 1.0 as the query language, but rather a proprietary one. Of course if you have always used that you can continue to use that, but there are XPath 1.0 expressions that throw errors that way. – Martin Honnen Oct 11 '13 at 15:37
  • A moment ago I also have faced the problem with the comparison `if (typeof ActiveXObject !== "undefined")`. I've gone crazy. If it weren't by the built-in debugger, I had never known if the class ActiveXObject really existed or not. So what I've done to fix my old scripts has been to create a function called `isActiveXSupported ()` with this code inside: `try { new ActiveXObject ("MSXML2.DOMDocument.6.0"); return true; } catch (e) { return false; }` . Then, I've replaced the old comparison with a call to this function. And my old scripts are still working well. – negora Sep 24 '14 at 11:38
0

To expand on pixelmatt's answer, some results of my tests (Win 7 64bit with IE11) I did in order to get DOMParser to work as it did in IE9 and IE10 (in IE11 it now returns an XMLDocument object which appears to not support xpath queries?).

Turns out I could make it behave like in IE10 with the following meta tag:

<meta http-equiv="X-UA-Compatible" content="IE=10" />

Results without and with above meta: IE11 default mode IE11 in IE10 mode

And here are the XMLDocument's memebers (for reference): enter image description here

pingo
  • 167
  • 2
  • 9
  • From memory this was a bit sneaky, the console would not print out the things you wanted but when you just try and run it in the code it works! I know we got this all working with out forcing IE11 to run in a compatibility mode as this meta tag will do. – pixelmatt Jan 16 '14 at 11:25
  • Without the meta tag, I could not get DOMParser to work as it used to (compare first and second image). Your way with ActiveXObject indeed works in both modes and is probably the better but in my case I needed DOMParses specifically and that was the only way to get it working again. I completely agree that the whole thing is sneaky in IE. – pingo Jan 16 '14 at 12:43