Generalized AJAX Response Handling for XML Content : Page 3

Making multiple simultaneous asynchronous requests in AJAX applications using the XmlHttpRequest object can be difficult, but this generalized XML response-handling code will help.

by Matt Warden

Jul 18, 2005

Page 3 of 3

The Solution: An Aperiodic Table of Elements
First, you can now remove the response type identifier from the preceding response XML document, because you no longer need it. That leaves a cleaner response as shown below.

Essentially, you want to map each element of interest in potential XML responses to its respective handling function. In JavaScript, built-in data structures are few and inadequate for this purpose (see the sidebar JavaScript Objects and Associative Arrays). Luckily, you can create your own data structure, in this case, a hashtable, to develop a mapping between element names and handler functions, as shown below:

These calls are all you need to attach handlers to elements in XML responses. Note also that the mapping includes elements from multiple response types.

With your handlers appropriately assigned, your library code could then loop through every element in the XML response and pass the element (including its children) to the appropriate hander, if it exists:

// this is within a loop over all elements
if (registeredHandlers[elementName])
{
// call the handler function.
registeredHandlers[elementName]();
}

The last line of the preceding code might look a little odd, but remember that the values of our mapping are literally function references.

Note that a response may contain many elements that do not have assigned handlers. In that case, the test for the element's name in the registeredHandlers array returns false and no call to a handler will be made. This is exactly what you want. In this way, every element of interest is passed to its relevant handling function and there is no need to further branch the code within the handling functions. As a byproduct, this scheme also solves the problem of handling different response types. In fact, you no longer care about response types, for handling purposes. You care only about the elements within the response. Of course, the element names registered should be unique. However, because the code passes a node object to the handling function, the function has access to all that node's properties, including parentNode. So, if it's unavoidable, you can write code to handle the same element name differently, based on its parent node(s). You can find an example of accessing the parentNode from within the handling function in the downloadable sample source.

By abstracting and encapsulating the XML communication specifics you can create AJAX library code such as the sample code for this article. Additionally, if ever you decide to use a method other than XMLHttpResponse to send requests to and receive responses from a remote server, you simply change the library code implementation and can leave the rest of your code untouched. AJAX opens up a number of new opportunities, but it is important not to leave tried and true programming principles behind in the process.

Matt Warden has been a freelance Web developer for six years. He focuses primarily on data modeling and server-side programming, but has recently begun incorporating fat client development models into his portfolio. He is active in the Web developer community, including local user groups and international online communities such as evolt.org.