Using XMLHttpRequest, reading XML data (Adobe AIR Application)
Categories: Programming
Tags: Adobe Air, xml, XMLHttpRequest
Written By: admin
You guys might know the importance of XML, XML (Extensible Markup Language) is a flexible markup language, meant to store, carry, and exchange data. We can make use of XML as a simple database to store data and retrieve data.
In this tutorial, you guys can learn how we can read XML data using XMLHttpRequest (Adobe AIR Application). Here is my sample XML data (empDetails.xml), I have created one root node called<employee> , in that I have created a node called <emp> and it has got one attribute called “id” and has got several child nodes like <name>, <designation>, <phone>. Now we have read these nodes using XMLHttpRequest and we need to display the same.
<employee> <emp id="1007"> <name>John Chamber</name> <designation>Web Expert</designation> <phone>555-55-555</phone> </emp> </employee>
Here is the code for reading XML data using XMLHttpRequest:
<script>
var xmlFile = null;
var xmlObj = null;
function doLoad()
{
xmlFile = air.File.applicationResourceDirectory.resolve("empDetails.xml");
xmlObj = new XMLHttpRequest();
xmlObj.onreadystatechange = function()
{
var elem = null;
var name = null;
var designation = null;
var phone = null;
var rootNode = null;
if( xmlObj.readyState == 4 )
{
rootNode = xmlObj.responseXML.documentElement.getElementsByTagName( "emp" );
for( var i = 0; i < rootNode.length; i++ )
{
name = rootNode[i].getElementsByTagName("name")[0].textContent;
designation = rootNode[i].getElementsByTagName("designation")[0].textContent;
phone = rootNode[i].getElementsByTagName("phone")[0].textContent;
elem = document.createElement("div");
elem.innerText = name + " " + designation;
document.body.appendChild(elem);
}
}
}
xmlObj.open( "GET", xmlFile.url, true );
xmlObj.send( null );
}
</script>
Hope the above code will helps the people who are working on Adobe AIR Applications. Cheers Guys!! Enjoy the World of Web!!








August 19th, 2007 at 6:35 pm
[...] Using XMLHttpRequest, reading XML data (Adobe AIR Application) » This Summary is from an article posted at Developer Snippets on Sunday, August 19, 2007 You [...]
August 26th, 2007 at 5:00 am
[...] Tutorials @ http://www.developersnippets.com/2007/08/19/using-xmlhttprequest-reading-xml-data-adobe-air-applicat... [...]
August 29th, 2007 at 8:40 pm
Hi Vivek,
Good article.. you have explained and given good ready to use code…
June 19th, 2008 at 9:15 am
Hi, thanks for the example, but can you please tell what is this variable “air” is? where does it come from? it is undefined if I try to use it… And your is not the 1at snippet where I ask myself thie question…..
Thank you very much
Natalia
June 19th, 2008 at 3:34 pm
Hi Natalia,
Thanks for posting your comments for this article, in the above example “air” is the parent class from which we are creating an object called “xmlFile”. I am thinking that you might be knowing about what is a ‘parent class’
Thanks,
Vivek