Step 4 – "The X-Files"
or Working with the XML Response
In the previous example, after the response to the HTTP request was received, we used the
reponseText property of the request object and it contained the contents of the
test.html file. Now let's try the
First off, let's create a valid XML document that we'll request later on. The document (test.xml) contains the following:
<?xml version="1.0" ?> <root> I'm a test. </root>In the script we only need to change the request line with:
... onclick="makeRequest('test.xml')"> ...Then in
alertContents()we need to replace the
var xmldoc = http_request.responseXML; var root_node = xmldoc.getElementsByTagName('root').item(0); alert(root_node.firstChild.data);
his way we took the
XMLDocument object given by
responseXML and we used DOM methods to access some data contained in the XML document. You can see the
test.xml here and the updated test script here.
For more on the DOM methods, be sure to check Mozilla's DOM implementation documents.
Validator and Debugger!