Flex/ActionScript3 - Object to XML
Working with Flex and Java as backend, I decided that the best choice for the project I’m working now is to simply post XML from Flex and get XML back from Java (Flex works really well with XML).
But there is a problem with that, creating a XML with one or two values is easy, but if you need to serialize a very large object tree you just got yourself a lot of work.
So I’ve wrote this very simple Flex XML Serializer, and decided to post it here, because some one might need some thing similar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package teste { import mx.utils.ObjectUtil; public class Utils { public static function objectToXml(obj : Object, name : String) : XML{ var result : XML; var info:Object = ObjectUtil.getClassInfo(obj); if(name==null) name = info.name; result = new XML("<" + name + "></"+ name + ">"); for each (var qn : QName in info.properties){ var val : Object = obj[qn.toString()]; if(ObjectUtil.isSimple(val)) result[qn.toString()] = val; else result.appendChild(objectToXml(val,qn.toString())); } return result; } } } |
To use is is really simple:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" initialize="appInit()"> <mx:Script> <![CDATA[ import teste.Utils; import mx.utils.ObjectUtil; import mx.controls.Alert; public function appInit() : void { var obj : Object = {name:'teste',address:{street:'rua',number:20}}; Alert.show(Utils.objectToXml(obj,'teste').toXMLString()); } ]]> </mx:Script> </mx:Application> |
The generated XML will look like this:
1 2 3 4 5 6 7 | <teste> <address> <number>20</number> <street>rua</street> </address> <name>teste</name> </teste> |
This might not be the best way to work, but I liked this solution for the problem I had
One thing I changed from this code to the code that is in production today is that I’m using XML attributes now, and as you can see below the change was big from the first example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | package teste { import mx.utils.ObjectUtil; public class Utils { public static function objectToXml(obj : Object, name : String) : XML{ var result : XML; var info:Object = ObjectUtil.getClassInfo(obj); if(name==null) name = info.name; result = new XML("<" + name + "></"+ name + ">"); for each (var qn : QName in info.properties){ var val : Object = obj[qn.toString()]; if(ObjectUtil.isSimple(val)) result['@' + qn.toString()] = val; else result.appendChild(objectToXml(val,qn.toString())); } return result; } } } |
Could not find the difference? the only change was the “‘@’ +” at line 16, and I was using attributes for the values ![]()
So, what do you think about this solution?
The next step is to write a deserializer and we’ll make XML rock the Flex world (just kidding :D)
If you enjoyed this post, make sure you subscribe to my RSS feed!





Hi,
Sorry to veer from the topic but, why dont you use AMF ?
And you dont have to right the code to serialize and desrializing the data(it is a big headache).
Granite Data Services is free, open source and above all it is for JAVA.
I have’nt used it on a project since I work with php and have been using AMFPHP for past one year in my webapp.
Though I was able to make Granite Data Services work on my local machine in an hour it is that simple
Mudit Tuli
Thanks or the tip,
I’m already using granite data services.
I found it about 2 hours after this post was published