How do you use it? This framework is easy to use: All you have to do is pass this library’s loadXMLDoc function, passing that function the URL to your XML source, the callback function to call after the XML is downloaded, and a true/false parameter that lets you remove white space in the downloaded XML (true means AJAXLib should remove white space). When the XML is downloaded, the XML will be accessible in a variable named resultXML.
How about an example? Say you wanted to read the text from a new XML document, message2.xml, which contains these contents:
<?xml version = "1.0" ?> <text> This text was fetched using AJAXLib. </text>
Doing so using the AJAXLib library is simple. You start, as you’d expect, by including ajaxlib.js in your Web page, testAJAXLib.html:
<html> <head> <title>Using the AJAXLib Ajax framework</title> <script src = "ajaxlib.js"></script> . . .
To download the XML, you can connect a button to the AJAXLib loadXMLDoc function, indicating that you want to download message2.xml, call a function named callback when the XML has been downloaded, and not remove white space in the XML:
<body> <H1>Using the AJAXLib Ajax framework</H1> <form> <input type = "button" value = "Get the message" onclick = "loadXMLDoc('message2.xml', callback, false)"> </form>
You’ll also need a <div> element to display the downloaded XML data:
<form> <input type = "button" value = "Get the message" onclick = "loadXMLDoc('message2. </form> <div > <p>The fetched data will go here.</p> </div>
Because there’s only one <text> element in the array created using getElementsByTagName, you can access that element as xmlData. The rest of the process for extracting the text data from that element is the same as with the libXmlRequest library, so here’s how you can display the downloaded data:
How does it look in practice? As you’d expect. Take a look at Figure 5.11, where the user has clicked the button and the loadXMLDoc function has done its thing, downloading the data, which has been displayed by the page. Very nice.
Figure 5.11: Downloading XML data using AJAXLib