Working with XML in ActionScript has been made pretty simple by E4X. But many developers prefer to work with ActionScript objects rather than straight XML. Unfortunately, converting to and from XML is an often laborious, and certainly tedious, task. Java developers may be familiar with Java Architecture for XML Binding (JAXB) and will quickly note that it was the inspiration for ActionScript XML Biinding (ASXB).
ASXB is a concrete implementation of XML binding that used custom metadata as the means of annotation. This is the 0.1 release of ASXB and so it’s fairly rough around the edges and is intended to test the waters, so to speak, of the ActionScript developer community. If the ASXB project seems like it might be of use in your development, please comment on it over in the arc90 blog.
What is this?
The ActionScript XML Binding (ASXB) project is intended to make the marshalling/unmarshalling of XML/ActionScript objects a much easier task. It’s a small library that allows a developer to use custom metadata to annotate his/her ActionScript objects for seamless marshalling and unmarshalling.
How Do I Use It?
To use ASXB in your Flex project, just follow these simple steps:
Download The Code
Click on the icon below to download the source code:
The file contains everything required to build the ASXB project, as well as a compiled release of the ASXB.swc.
Add the code to your Flex or AIR project
You can either add the compiled library, ASXB.swc, to your project’s library path, or include the source directly in your project.
Add compiler settings
Copy flex-config-template.xml to your project and change the name to flex-config.xml. Add the following compiler setting: -load-config+=<path-to-config>/flex-config.xml
Annotate your ActionScript objects
Below is a very basic example of an annotated object.
[XmlRootElement(name="book")] public class Book { [XmlAttribute(name="authorFullName")] public var author:String; [XmlAttribute] public var dateOfPublication:Date; [XmlAttribute(name="pageCount")] public var pages:int; [XmlElement] public var title:String; ... }
The above object maps to the following XML snippet. When the name of an annotated member is the same as the corresponding XML element or attribute, no name key/value pair is required.
<book authorFullName="Alastair Reynolds" dateOfPublication="2003-5-27" pageCount="704"> <title>Chasm City</title> </book>
Create an instance of XMLService using either MXML or ActionScript.
MXML:
<arc90:XMLService id="service" url="http://example.url/service/" fault="faultHandler(event)" result="resultHandler(event)"/>
ActionScript:
import org.orpheus.xml.bind.XMLService; var service:XMLService = new XMLService(); service.url = "http://example.url/service/" service.addEventListener(FaultEvent.FAULT, faultHandler); service.addEventListener(ResultEvent.RESULT, resultHandler);
XMLService functions exactly like mx.rpc.http.HTTPService
To send data to a service just call send, passing an ActionScript object as the parameter, or by setting the request property.
var book:Book = new Book(); book.author = "Alastair Reynolds"; book.title = "Chasm City"; service.send(book); // or service.request = book; service.send();
Any data returned as a result of a service call will be automatically unmarshalled into the appropriate data type. It is accessible via the result property of the ResultEvent object.
private function resultHandler(event:ResultEvent):void { var book:Book = event.result as Book; trace("You have retrieved " + book.title + " by " + book.author + "."); // OUTPUT: You have retrieved Chasm City by Alastair Reynolds. }
Documentation
Review the complete documentation for a full list of all properties, methods, and annotations of the ActionScript XML Binding project.
Licensing
This arc90 tool is licensed under the BSD license
Discuss ActionScript XML Binding
You can offer feedback on ActionScript XML Binding at the arc90 blog.
