The parsing and processing of XML response data from various sources (especially, SOAP messages returned from web services via the
XMLHttpResponse object) is becoming a more and more common task in the browser. The idea behind the solution presented in this article is to ease this parsing and processing by providing a simple and extensible solution to transform your
Using the code
Using the messageManager is easy. Simply include the messageManager.js file in your page.
Create an instance of the object:
var messageMan = new MessageManager();
Add in the maps; these link the XML node in the response data to the object that you wish to create and populate. For example, if your response data contained the following node:
...and you wanted to create a '
jsUser' object from this noden you would create the map as follows:
Next, you would create and send your message to the web service (in your preferred manner) and then pass the
responseXML into your
messageManager as follows (if
oXmlHttpRequest is the name of your
Note: the return type of
MessageManager.processMessage(responseXML) is a hashtable (as implemented in the HashTable.js file). The hashtable contains a collection of key/value pairs, with the key being the ID of the created object and the value being the object itself.
Points of interest
this.id = node.selectSingleNode("Name").text;
jsUser.prototype.fromXML = function(node)
this.name = XMLUtil.getSingleXpathValue(node, "Name");
this.id = this.name;
this.firstname = XMLUtil.getSingleXpathValue(node, "FirstName");
this.lastname = XMLUtil.getSingleXpathValue(node, "LastName");
this.branch = XMLUtil.getSingleXpathValue(node, "Branch");
this.department = XMLUtil.getSingleXpathValue(node, "Department");
this.winuser = XMLUtil.getSingleXpathValue(node, "WinUser");
this.unixuser = XMLUtil.getSingleXpathValue(node, "UnixUser");
this.phone = XMLUtil.getSingleXpathValue(node, "Phone");
Hopefully, the source code attached and demo provided should be fairly self-explanatory; the code is quite heavily commented. I would suggest downloading the demo first (it should work straight out of the box with the sample web service included).
Finally, many thanks to my friend Ian Harrigan on whose Java implementation the idea was based.