1. Computing

Introduction to Ajax

11. Parsing XML

clr gif

Once you have got your response back from the server you need to break it up into its component parts (assuming that there is more than one pices to the information) and then use the DOM to update the web page with the information. Where we get the respose from the responseText field (which is always available) we then have to parse the content ourselves in order to determine what parts of the content are what.

When we retrieve XML from the server we have both the responseText and the responseXML fields being populated and the advantage to using responseXML is that the work of parsing the XML is done for us.

Presumable since both the Javascript that sends the request and the server side processing that builds the response based on that request are both on our web site we should know what XML fields can or might be passed back in the response. We can then use methods on the responseXML field to retrieve the content of specific fields using near identical code to that which we would use to retrieve the content of certain containers within our web page. For example if we are exmecting a number of entries inside <price> </price> tags in the XML then we can extract them into a Javascript array using:

var xmlDoc = request.responseXML;
var priceArray = xmlDoc.getElementsByTagName('price');

To extract the information from the XML document into Javascript for processing is effectively identical to how we would retrieve information from the web page for processing via the DOM except for the names of the containers.

©2014 About.com. All rights reserved.