Learn Photoshop, Flash, Adobe AIR, ExtJS, jQuery, Ajax, Dojo, HTML, CSS, JavaScript, XML, Accessibility, Database, DWR, Gears, GWT, Java, JSON, MooTools, Office, Perl, PHP, Programming, Prototype, Scriptaculous and more

Want to explore your choice of video from all over world at once place! then what are you waiting for, just click, explore and learn.

Tech Video Bytes

Using XMLHttpRequest, reading XML data (Adobe AIR Application)

Categories: Programming
Tags: , ,
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!!

5 Responses to “Using XMLHttpRequest, reading XML data (Adobe AIR Application)”

  1. University Update - AJAX - Using XMLHttpRequest, reading XML data (Adobe AIR Application) Says:

    [...] Using XMLHttpRequest, reading XML data (Adobe AIR Application) » This Summary is from an article posted at Developer Snippets on Sunday, August 19, 2007 You [...]

  2. XMLHttpRequest, reading XML data in AIR | Adobe AIR Tutorials Says:

    [...] Tutorials @ http://www.developersnippets.com/2007/08/19/using-xmlhttprequest-reading-xml-data-adobe-air-applicat... [...]

  3. Ravinder Says:

    Hi Vivek,

    Good article.. you have explained and given good ready to use code…

  4. Natalia Says:

    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

  5. Vivekanand Says:

    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

Leave a Reply

Featured & Popular Articles