Fast Loading Of XHTML as XML In JavaScript Using Msxml2.DOMDocument.*

This solved the issue I was having with MSXML not parsing XHTML as XML without an xml-declaration – which is the only format that Microsoft Expression Web will auto-detect as XHTML without forcing it to fail over to it. ([menu] Tools > Page Editor Options > Authoring > Secondary Schema = XHTML 1.0 Strict).

I coded up a simple test case

Normally for all things XML in Javascript I have a handy lib that normally abstracts this away for me but for a test-case I’m staying close to the metal.

var l_xml_document = new ActiveXObject("Msxml2.DOMDocument.3.0");
l_xml_document.async = false;
l_xml_document.validateOnParse = false;
l_xml_document.resolveExternals = false;

I’ll run through each line

var l_xml_document = new ActiveXObject("Msxml2.DOMDocument.3.0");

This creates an Object which is IE’s version of the W3C standard DOM Document. I ask for the MSXML 3.0 DOM Document by name because its the default for “Msxml2.DOMDocument” . This means I’m not going to get any surprises if the default goes to v6.0 and it changes behavior. V3 seems to be installed on all Windows machines by default. From memory it came as part of IE5.5

l_xml_document.async = false;

This disables asynchronous mode – in this case I’m happy to wait for the file to arrive

l_xml_document.validateOnParse = false;

This was the clincher – without setting validateOnParse to false I was unable to parse XHTML in the following format (which is valid XML – the xml-declaration is optional according to the w3c spec)

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="" lang="en"><head></head><body></body></html>

I needed to add the xml-declaration at the start.

<?xml version="1.0" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">
<html xmlns="" lang="en"><head></head><body></body></html>

When I did add the xml-declaration I had to force Microsoft Expression Web to fail-over this as XHTML, it would not detect it.

Using either the ‘xml-declaration’ or ‘validateOnparse set to false’ workarounds, DOMDocument was very slow to load the XML, which brings us to the next line

l_xml_document.resolveExternals = false;

This prevents Msxml2.DOMDocument from trying to load and DTD’s and validate your code. This can take 5 seconds per request – so you really don’t want this unless there is a real danger that the XML you get back will be not conform to XHTML.


Load it. Not explaining this.

Hope this helps someone. Special thanks to John-Daniel Trask and Chris Bentley who commented on my original post.


About metawrap

CTO Massive Interactive. Ex Computer Whiz Kid - Now Grumpy Old Guru.
This entry was posted in JavaScript, XML. Bookmark the permalink.

One Response to Fast Loading Of XHTML as XML In JavaScript Using Msxml2.DOMDocument.*

  1. Andy says:

    Hi, thanks for this. It helped me fix a completely different problem (trying loading a XML file I kept getting ‘null’). Luckily I came across this and the ‘validateOnParse = false’ fixed the issue.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s