Cross-browser XML and JavaScript

During my work on XML Serializer I ran into the problem with differences with XML support in browsers. Hopefully, this problem now is much clearer than in early days and you do not need a special library, like Sarissa, to work with XML nodes in modern browsers. Things are clear and nice now, because the standard DOM is implemented in latest Mozilla/Firefox, Opera and Safari.

However, there are still a few minor changes with loading and serializing XML documents and
I have summarized them in the following adapter:

function getDomAdapter()
{
	var adapter = '';
	if ('undefined' != typeof ActiveXObject) {
		adapter = 'MS';
	} else if ('undefined' != typeof document
		&& document.implementation
		&& document.implementation.createDocument
		&& 'undefined' != typeof DOMParser)
	{
		adapter = 'default';
	}
	switch (adapter) {
		case 'MS':
			return new (function () {
				this.createDocument = function () {
					var names = ["Msxml2.DOMDocument.6.0",
						"Msxml2.DOMDocument.3.0", "MSXML2.DOMDocument",
						"MSXML.DOMDocument", "Microsoft.XMLDOM"];
					for (var key in names) {
						try {
							return new ActiveXObject(names[key]);
						} catch (e) {}
					}
					throw new Error('Unable to create DOMDocument');
				};
				this.serialize = function (doc) {
					return doc.xml;
				};
				this.parseXml = function (xml) {
					var doc = this.createDocument();
					if (!doc.loadXML(xml)) {
						throw new Error('Parse error');
					}
					return doc;
				};
			})();
		case 'default':
			return new (function () {
				this.createDocument = function () {
					return document.implementation.createDocument("", "", null);
				};
				this.serialize = function (doc) {
					return new XMLSerializer().serializeToString(doc);
				};
				this.parseXml = function (xml) {
					var doc = new DOMParser().parseFromString(xml, "text/xml");
					if ("parsererror" == doc.documentElement.nodeName) {
						throw new Error('Parse error');
					}
					return doc;
				};
			})();
		default:
			throw new Error('Unable to select the DOM adapter');
	}
};

The function above creates adapter that is suitable for current browser and
contains the following methods:

createDocument()
creates blank XML DOM Document.
serialize(DOMDocument)
dumps XML DOM Document to a string.
parseXml(string)
parses XML string and returns XML DOM Document, throws expcetion if parsing fails.

This is an example on use of the adapter:

var xml = "";
var doc = getDomAdapter().parseXml(xml);
doc.documentElement.appendChild(doc.createElement('passed'));
alert(getDomAdapter().serialize(doc));

This code successfuly displays "<test><passed /></test>" in the following browsers:

  • Mozilla Firefox 2
  • Internet Explorer 7
  • Opera 9
  • Safari 3 (Win)

Comments

Help!!

Hi Alex,
Thanks for the script. I have a XML doc that I need to load into a combo box, and it needs to work with Safari. However, I couldn't manage it. I am tring to run and get ideas from your script but I couldn't make it run either. It throws me "Parse Error" in case of MS. I just copied your script into a html doc. and did nothing else. Can you help me to run it so that may be I can use it in my real problem?

Thanks again,

Gabriel

Hello

First, posting the question on the forum increases the chance that I get it quickly :-) As for your problem, unfortunately I cannot reproduce it by your instruction. It would be great if you send me the file I can run and check the error. Please note that I use Safari for Windows.

Sincerely,
Alex

Flawless

Just tried this out with Firefox and IE7 and it works perfectly. Superb job, thanks very much!

Thank you

This is the most useful bit of code I've seen on the web today! Nice job and thank you!