Parsing prototype’s responseXML in JavaScript

Here’s a quick code snippet to parse the responseXML using JavaScript.

I am printing the xml file using php, feel free to use your server side scripting language.

First make a quick Ajax.Request call to send the data to server file.

function AddItem() {

 
var input = ‘myinput=’+$F(‘myinput’);
var list = ‘ListID=’+$F(‘ListID’);
var user = ‘userID=’+$F(‘userID’);

var pars = input+’&’+user+’&’+list;

new Ajax.Request(

‘GetItem.php’,
 { 
  asynchronous:true,
  parameters:pars,
  onComplete: ShowData 
  
 }
);
$(‘myform’).reset();
$(‘myinput’).activate();
return false;

}

Now that we have sent the data to our server file, in the above case its GetItem.php

Print the xml file in the php side.

Collect the reponse from the server in the form of the responseXML.

Below is the code to parse the responseXML and picking it up in javascript DOM.

function ShowData(originalRequest) {
 
var xmlDoc = originalRequest.responseXML.documentElement;

var value = xmlDoc.getElementsByTagName(“ItemValue”)[0].childNodes[0].nodeValue;
var value1 = xmlDoc.getElementsByTagName(“ItemID”)[0].childNodes[0].nodeValue;

divID = ‘DIV’+value1;

var div = document.createElement(‘div’);
div.className =’ItemRow’;
div.id = divID;

var val = ‘”‘+value+’”‘;

var i = document.createElement(‘input’);
i.type=’checkbox’;
i.id=value1;
i.value=value;
i["onclick"] = new Function(“MarkDone(this.id)”);
var t = document.createTextNode(value);

div.appendChild(i);
div.appendChild(t);

$(‘ItemTree’).appendChild(div);

new Effect.Highlight($(div));
}

This has worked perfectly for me, let me know if you run into issues with this code.

Cheerz

About these ads

About srinix

I am Sridhar, a matured nut, on this planet with water, air and love ;)

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

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: