Ajax responseXML


Send an ajax request to server and get the server response as XML. After receiving the response xml object, you can parse it using W3C DOM methods.Lets see the below example.

response XML like this.

<ProductDetails>
<ProductName>NetBeans</ProductName>
<ProductDescription>Java/Jsp Editior</ProductDescription>
<ProductPrice>120</ProductPrice>
</ProductDetails>

There are two ways to parse the xml.
var proddet = getElementsByTagName(“ProductDetails”)[0]; – retrieve the product details using parent node name.
method 1 :
proddet .childNodes[0].childNodes[0].nodeValue – retrieve the details using index of child nodes.
method 2 :
proddet .getElementsByTagName(“ProductName”)[0].firstChild.data – retrieve the details using corresponding child node name.

Method 2 is best comparing to method 1. Because retrieve the value with index is bit confusing and also not easy to assign the node value to corresponding  name.

Example.

if (req.readyState == 4) {
if (req.status == 200) {
var response = req.responseXML;
var proddet = response.getElementsByTagName(“ProductDetails”)[0];
var prod_name = proddet .getElementsByTagName(“ProductName”)[0].firstChild.data;
var prod_desc = proddet .getElementsByTagName(“ProductDescription”)[0].firstChild.data;
var prod_price = proddet .getElementsByTagName(“ProductPrice”)[0].firstChild.data;
// you can write your code here to show the retrieved data.
}
}

Ajax Basics – https://manikandanmv.wordpress.com/2008/09/23/ajax-asynchronous-javascript-and-xml/

Ajax Example – https://manikandanmv.wordpress.com/2008/09/29/ajax-example/

Advertisements

Tags: , , , , , ,

2 Responses to “Ajax responseXML”

  1. Nkhil Says:

    we need to call a function that will be returned by the ajax …. Could you post an example of this file. I mean the codes

  2. manikandanmv Says:

    I think, you are asking about calling javascript functions in ajax response. Please see invokeScript method in the below link https://manikandanmv.wordpress.com/2008/09/29/ajax-example/

    Hope this clarifies your question.!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: