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