Navigation
Tags
« Water Ripples | Main | Wunderkind BVR »
Sunday
Sep192010

XML parsing with FlexXB

One of the necessary evils of working with xml in Flash is the mundane task of writing xml parsers. I've written countless xml to object and object to xml functions over the years. That is until I came across FlexXB.

FlexXB is a library for handling automatic xml (de)serialisation within Flash. There are a few xml serializers out there including the XML to Object Mapper that's built into Spicelib which is the core of Parsley (my new favourite application framework). Most of these libraries however require you to write quite verbose actionscript to utilize their most basic functions. They end up being hard to write, read and maintain in my opinion. FlexXB can create mappings this way too, but utilizing it within Flex is where it really comes into its own. There you can easily decorate your classes with simple as3 metadata tags that define the mappings automagically.

Here is an example taken from the documentation:  

This is a simple example on using xml annotations. Only fields that have been decorated with an annotation will be taken into account in the (de)serialization process. Notice the idField attribute. It specifies that this object supports a compact method of defining it in xml, by only specifying the id. The id will be rendered as element or attribute in the owner xml.

        [XmlClass(alias="Mock2Replacement", idField="id")]
       
public class Mock3
       
{
               
[XmlAttribute]
               
public var id : Number = 3;
               
[XmlAttribute]
               
public var attribute : Boolean;
               
[XmlElement(alias="objectVersion")]
               
public var version : Number;
               
               
public function Mock3()
               
{
                       
super();
               
}
       
}

The object has been built as:

        var target : Mock3 = new Mock3();
        target
.attribute = true;
        target
.id = 5;
        target
.version = 33;

The resulting XML is:

        <Mock2Replacement attribute="true" id="5">
         
<objectVersion>33</objectVersion>
        </Mock2Replacement>

This makes it so easy to use as classes can be configured inline and most of the time all you need is a simple [XmlElement] tag before a property and you're good to go. If there is ever a whole lot of logic to how an object is (de)serialised all you have to do is implement the XMLSerializer interface and you can do your work in the encode and decode functions. The options at your disposal within the tags gives you lots of power though with support for attributes, elements, array handling, class names, namespaces, virtual xml paths, id referencing, object caching and more. As well as creating mappings for custom objects you can create Convertors for simple objects like Dates that (de)serialize to/from strings.

There are some rare cases where you don't have access to the classes you want to (de)serialize (such as from a swc) which means you can't annotate them. But FlexXB has you covered with an easy to use api for programatically configuring your mappings. There are a few other features of the library such as classes for communicating with web services and object persistence, but I won't go into those here.

The developer recently announced details about an upcoming version (1.7) which will include more robust caching support including a feature I asked for only recently. He has also talked about adding support for xml versioning, which would allow for different xml mappings based on the version specified.

All I can say is this library has saved me hours and hours of time and recommend it highly. I have yet to see something that handles the mundane problem of xml (de)serialization so elegantly.

PrintView Printer Friendly Version

EmailEmail Article to Friend

Reader Comments (1)

Hello Yonas,

Thanks for the nice review. I am very glad this library has proven to be a big help for you and I hope future releases will continue to confirm this opinion. Flexxb has many things to say regarding serialization and I am trying to make time to address all the ideas i have for it.
One evolution I plan for FlexXB is to make it suport different formats of serialization besides XML. A pluggable architecture that would allow a developer to quickly write a plugin to make FlexXB work with JSON for example. Thus the core features (caching, id referencing, field ignore capabilities etc.) will be consistent regardless of the serialization medium.

Whenever you need assistance with the library or just want to speak your mind on the subject be sure to check out the discussion group and I will be happy to help, as always.

Have a good day,
Alex

December 9, 2010 | Unregistered CommenterAlex Ciobanu

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>