By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,626 Members | 2,202 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,626 IT Pros & Developers. It's quick & easy.

Adobe Flex 2.0 confusions

P: 1
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.
Oct 5 '07 #1
Share this Question
Share on Google+
1 Reply


xNephilimx
Expert 100+
P: 213
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:

Expand|Select|Wrap|Line Numbers
  1. import flash.net.URLLoader;
  2. import flash.net.URLLoaderDataFormat;
  3. import flash.net.URLRequest;
  4.  
  5. private function LoadXML():void { //first load the file as you normally would
  6.     var loader:URLLoader = new URLLoader();
  7.     loader.dataFormat = URLLoaderDataFormat.TEXT;
  8.     loader.addEventListener(Event.COMPLETE,onComplete);
  9.     loader.load(new URLRequest("thexml.php")); //I put php since I use this language. I make it return an XML.
  10. }
  11.  
  12. private function onComplete(e:Event):void {
  13.     try {
  14.         var xml:XML = new XML(event.target.data);
  15.         // here you can start parsing your XML file with the E4X model
  16.     } catch (e:TypeError) {
  17.         //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
  18.     }
  19. }
  20.  
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):

Expand|Select|Wrap|Line Numbers
  1. <employees>
  2.     <employee id="1">
  3.         <name>Guybrush Threepwood</name>
  4.         <position>Pirate</position>
  5.     </employee>
  6.     <employee id="2">
  7.         <name>LeChuck</name>
  8.         <position>Ghost Pirate</position>
  9.     </employee>
  10. </employees>
  11.  
So the AS3 function onComplete would do something like this:

Expand|Select|Wrap|Line Numbers
  1. //I'mm ommiting the try-catch block just to speed this typing up XD
  2. private function onComplete(e:Event):void {
  3.     var xml:XML = new XML(e.target.data)
  4.  
  5.     //to access each employee, just loop through the xml:
  6.     for each(var elem:XML in xml.elements()) {
  7.         //to acces an element's attribute:
  8.         elem.@id;
  9.         //to access an element's content:
  10.         elem.employee.name;
  11.         //to asign it to something you should convert the value to the correct type:
  12.         var eName:String = elem.employee.name.toString();
  13.         var id:Number = Number(elem.@id);
  14.     }
  15. }
  16.  
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.
Oct 5 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.