Tuesday, September 9, 2008

Javascript with Xml

We can easily make AJAX call to backend web application, but to consume the value responded isn't a easy job. We need to have a protocol for the data transfered from server to browser. The protocol can be done with XML or JSON. Here I will show a example with xml data protocol.




wanderer
Walter Jeffries
Sally Jacobs




Javascript load a xml file
into memory and create an object for accessing

var myXmlDoc = new ActiveXObject("Microsoft.XMLDOM");
myXmlDoc.async = false;
myXmlDoc.load("exampleDoc.xml");



// access elements
myXmlDoc.childNotes(0).childNodes(1).tagName;
myXmlDoc.childNotes(0).getAttribute("raceNo");
myXmlDoc.childNotes(0).childNotes(1).text;




var myRequest = getXmlHttpRequest();
myRequest.open("GET", url, true);
myRequest.onreadystatechange = callbackFunc;
myRequest.send(null);

readystate:
1- unitialized
2 - loading
3 - interactive
4 - completed

if(myRequest.readyState == 4)
{
if(myRequestStatus == 200)
{
var greetNode = http.responseXml.getElementByTagName("greeting")[0];
var greetText = greetNode.childNodes[0].nodeValue;
}
}

** here http should be myRequest?!

using the returned data

document.getElementById() ...

No comments: