Hi abbasnaqvi!
Flex 2 uses ActionScript 3, and AS3 introduces a "new" (different from AS2) way of handling XML files, the E4X model. I didn't try it thoroughly, but up to now it seems a pretty strong and stable way to handle XML files.
Your best choice is probably to make a server side script to manage a database and when retriving the data, use XML, I mean, make your server side script return an XML that you will read from your flex app.
To load an XML use the URLLoader Class, set its data format to URLLoaderDataFormat.TEXT, so you retrive the XML as plain text and add a listener to your URLLoader instance to watch for the Complete event, where you will create an XML instance from the text returned from the server.
Something like this:
-
import flash.net.URLLoader;
-
import flash.net.URLLoaderDataFormat;
-
import flash.net.URLRequest;
-
-
private function LoadXML():void { //first load the file as you normally would
-
var loader:URLLoader = new URLLoader();
-
loader.dataFormat = URLLoaderDataFormat.TEXT;
-
loader.addEventListener(Event.COMPLETE,onComplete);
-
loader.load(new URLRequest("thexml.php")); //I put php since I use this language. I make it return an XML.
-
}
-
-
private function onComplete(e:Event):void {
-
try {
-
var xml:XML = new XML(event.target.data);
-
// here you can start parsing your XML file with the E4X model
-
} catch (e:TypeError) {
-
//if we get here, then the file is not properly formatted as an XML, so it cannot be converted to an XML instance. So do something when you get here, maybe trace something, I don't know
-
}
-
}
-
Parsing an XML with this E4X syntax is very simple, use the elements() method to get all the elements, so you can loop through with a for each (yes, in AS3 they finally included a for each structure, though it works the same as the AS2 version, but wth the fancy name).
Suppose you have an XML like this one (just as an example):
-
<employees>
-
<employee id="1">
-
<name>Guybrush Threepwood</name>
-
<position>Pirate</position>
-
</employee>
-
<employee id="2">
-
<name>LeChuck</name>
-
<position>Ghost Pirate</position>
-
</employee>
-
</employees>
-
So the AS3 function onComplete would do something like this:
-
//I'mm ommiting the try-catch block just to speed this typing up XD
-
private function onComplete(e:Event):void {
-
var xml:XML = new XML(e.target.data)
-
-
//to access each employee, just loop through the xml:
-
for each(var elem:XML in xml.elements()) {
-
//to acces an element's attribute:
-
elem.@id;
-
//to access an element's content:
-
elem.employee.name;
-
//to asign it to something you should convert the value to the correct type:
-
var eName:String = elem.employee.name.toString();
-
var id:Number = Number(elem.@id);
-
}
-
}
-
Of course this code does nothing at all, it's just an example. I'm sure you'll be able to adapt this to your project.
Best regards,
The_Nephilim
Hi All,
I am developing Employee Management system in Adobe Flex and
I am coding a web service to get data from the database and return
it to Adobe Flex application
but I can find a better way to handle my data in Adobe Flex application.
Is there any sample project to do that, then it will be very helpfull.
Or if anyone can suggest some technology to receive and handle data in
Adobe Flex.
If I want to write data to xml and return to Adobe Flex application then
how well can it handle XML?
Can we serialize xml to classes in Adobe Flex?
Thankyou, for your instant replies.