views:

144

answers:

2

In JavaScript, xmlHttpRequest.responseXML() returns a DOM Document object. The DOM Document object is created from an XML-structured HTTP response body.

At what point during the life of an xmlHttpRequest object is the XML string parsed into the DOM Document?

I can imagine it may occur in one of two places.

  • When responseXML() is called.
    No need to waste resources parsing the XML string into a DOM until you know it's actually needed.

  • When the HTTP response has been received.
    If the server returns a text/xml content-type, it's clear you've requested XML and you're probably going to want the response body parsed into a DOM as you otherwise can't do much with the requested data.

Both options have some merit, although I'm inclined to say that the XML string is parsed only when responseXML is called.

At what point does parsing of the XML string occur?

Reaons for asking: I need to measure browser-based XML deserialisation performance, with the aim of comparing this to JSON deserialisation performance.

+1  A: 

I wouldn't be surprised if this is browser dependent. Why not profile all three?

TokenMacGuy
+1  A: 

It would make a great deal of sense for the stream to be parsed as its received. Waiting until the response is complete (or the responseXml property is called) means an extra delay is introduced between receiving the final bytes and the DOM being built. It would seem a better approach would be to build the DOM in parallel with receiving the stream, hence on completion the DOM is ready for use.

Note a big clue is in the fact that MSXML doesn't provide a DOM if the Content type doesn't specify the an XML type and Mozilla's implementation allows you to tell it to treat the content received as if it were XML despite Content-Type indicating otherwise.

Neither of the above be necessary if the DOM isn't parsed until the property is accessed. The property may as well simply attempt the parse. Its because a DOM may be built as the content is received that the above is necessary.

AnthonyWJones