Whoever uses AJAX already felt the need of an easy way to extract data from a XML response send by the server. Probably many already created one.
The usual way to work with the XML response is:
[SIZE=3]document.getElementById(“name”).innerHTML = XmlHttpRequest.responseXML.getElementsByTagName(“name”)[0].childNodes[0].nodeValue;
It is not really long but think how big will be your script after ten, twenty, thirty lines like this. Some people to reduce the code make like this:
[SIZE=3]var xmlresponse = XmlHttpRequest.responseXML;
document.getElementById(“name”).innerHTML = xmlresponse.getElementsByTagName(“name”)[0].childNodes[0].nodeValue;
But this isn’t really much short so this is my way to reduce it. First a very little function.
[SIZE=3]function getXMLData(objXmlHttp,tag) {
return objXmlHttp.responseXML.getElementsByTagName(tag)[0].childNodes[0].nodeValue;
}
new code now will look like this:
[SIZE=3]document.getElementById(“name”).innerHTML = getXMLData(XmlHttpRequest,”name”);
for a code even shorter why can’t we reduce the way we get the field?
[SIZE=3]function getField(id) {
return document.getElementById(id);
}
Our new code will be like this.
[SIZE=3]getField(“name”).innerHTML = getXMLData(XmlHttpRequest,”name”);
Hope some of you like this. If you post somewhere else give at least the credits. Got this simple(silly) idea working on my thesis about AJAX.