DOMParser.parseFromString() in Explorer

28 May 2007

Another bit of JavaScript goodness. Gecko-based browsers (and some others) have a beautifull feature called DOMParser. With this function it is trivial to parse a string containing XML and handle it as a DOM object. This is extremely handy in a myriad of cases. Unfortunately, Internet Explorer does not support it. I've been told that older versions of Safari also won't eat it. So that might also be true for older Konqi's.


Not to worry. Both cases have some form of architecture which makes it possible. But we don't want repeating, obfuscating code. And we don't want browser-detects. Here is the deal:

  1. if(typeof(DOMParser) == 'undefined') {
  2.  DOMParser = function() {}
  3.  DOMParser.prototype.parseFromString = function(str, contentType) {
  4.   if(typeof(ActiveXObject) != 'undefined') {
  5.    var xmldata = new ActiveXObject('MSXML.DomDocument');
  6.    xmldata.async = false;
  7.    xmldata.loadXML(str);
  8.    return xmldata;
  9.   } else if(typeof(XMLHttpRequest) != 'undefined') {
  10.    var xmldata = new XMLHttpRequest;
  11.    if(!contentType) {
  12.     contentType = 'application/xml';
  13.    }
  14.    xmldata.open('GET', 'data:' + contentType + ';charset=utf-8,' + encodeURIComponent(str), false);
  15.    if(xmldata.overrideMimeType) {
  16.     xmldata.overrideMimeType(contentType);
  17.    }
  18.    xmldata.send(null);
  19.    return xmldata.responseXML;
  20.   }
  21.  }
  22. }

When you put this (near) the top of all executed JavaScript on a site, you can safely use DOMParser.parseFromString(). It's quite simple: if the function DOMParser does not exists, it creates it. It then mimmicks the functionality by trying the ActiveX equivalent. If that does not work, it tries to imitated the behaviour by using a XMLHttpRequest using the data:\\ URI. There is only one pitfall: DOMParser should be undefined. Not a string, not an object, not a function, undefined. Unless it already works, offcourse.




Comments

DOMParser.parseFromString() in Explorer

Your code is really great and helpful !

Thank you.

Vincent

By Vincent (not verified) at Wed, 16/12/2009 - 9:56pm | reply

DomParser.parseFromString missing encodeURIComponent code

I tried this and it didn't work as an object (I kept getting ...not supported), so I converted it to a simple function. It works with Internet Explorer, but it hangs up in Firefox on xmldata.open... and I assume it's (at least partially) because encodeURIComponent is undefined. Please send me the code for that function and/or change this page to include it. This was the only thing I could find on the internet that addresses this subject (thanks very much), but it is useless without this missing piece because I still can't get the functionality in FireFox browser. Thanks.

By Aaron Meyer (not verified) at Wed, 20/06/2007 - 3:03pm | reply

That's not the way :-)

This is not needed in Firefox, since it already has the DOMParser function. That is why I stated that this was available on Gecko-based browsers. Hence the if(typeof(DOMParser) == 'undefined'), which makes sure that DOMParser only gets defined when it is not yet defined. In Firefox it is, so changes are that you are replacing it. Don't :-)

The resulting "thing" should be a function, not an object. I tested this over and over, and when you place the code unaltered in the top of your page, virtually every browser that supports JavaScrip will work with it.

Example of use:

  1. var xmlString = "<root><thing attr='val'/></root>";
  2. var parser = new DOMParser();
  3. var doc = parser.parseFromString(xmlString, "text/xml");

doc is then a reference to a DOM-object, containing the parsed XML.

By FST777 at Wed, 20/06/2007 - 6:30pm | reply

Comments