The most important feature of the Ajax web application architecture is its ability to script HTTP with the XMLHttpRequest object, which was covered in Chapter 20. The X in "Ajax" stands for XML, however, and for many web applications, Ajax's use of XML-formatted data is its second most important feature.
21.1 Obtaining XML Documents
Chapter 20 showed how to use the XMLHttpRequest object to obtain an XML document from a web server. When the request is complete, the
responseXML property of the XMLHttpRequest object refers to a Document object that is the parsed representation of the XML document. This is not the only way to obtain an XML Document object, however. The subsections that follow show how you can create an empty XML document, load an XML document from a URL without using XMLHttpRequest, parse an XML document from a string, and obtain an XML document from an XML data island.
21.1.1 Creating a New Document
You can create an empty (except for an optional root element) XML Document in Firefox and related browsers with the DOM Level 2
methoddocument.implementation.createDocument(). You can accomplish a similar thing in IE with the ActiveX object named
MSXML2.DOMDocument. Example 21-1 defines
anXML.newDocument() utility function that hides the differences between these two approaches. An empty XML document isn't useful by itself, but creating one is the first step of the document loading and parsing techniques that are shown in the examples that follow this one.
Example 21-1. Creating an empty XML document