AJAX - The Complete Reference

(avery) #1

96 Part I: Core Ideas


parser returns an XML tree with a root node of <parseerror> that contains information
about the parse error encountered, as shown in Figure 3-6.
Assuming no syntactical errors are made in the XML, you might desire to dive in and start
using the data, but that begs yet another question: is the actual returned data valid? What this
means is that it is important to not only look to make sure that the various tags found in the
response are syntactically well formed, but also whether they are used properly according to
some defined Document Type Definition (DTD) or schema. Unfortunately, by default, XHR
objects do not validate the contents of the responses. This can be addressed by invoking a
DOM parser locally in some browsers like Internet Explorer, but in others it isn’t possible to
validate at all, which eliminates some of the major value of using XML as a data transmission
format. We will pursue this issue in greater detail when data formats are covered in the next
chapter, but to explore all the variations of MIME types, well formedness, and validity now,
you can use the example at http://ajaxref.com/ch3/xmlrequestexplorer.html.

XML Challenges and Benefits
There are a number of other challenges facing those who wish to use XML as a response format
even beyond what has been mentioned here. XML may be a bulkier format and need
compression and for large data sets may have local parsing time consideration. Partial
responses are pretty much out of the question when using XML, but obviously there aren’t
only downsides to the format. The various tools such as Xpath and XSLT to consume the
received content are quite powerful. Further, the ability to generally validate the syntactical
and semantic integrity of a received data packet is certainly quite appealing. Yet our goal in this
chapter is primarily to focus on the XHR object itself, so let’s return to that discussion directly.

FIGURE 3-6
responseXML
parse tree in
Firefox on error
Free download pdf