Using XMLHttpRequest, reading XML data (Adobe AIR Application)
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!!