|
Project Information
Featured
Downloads
Links
|
ASAXB - BANG! AND THE CODE IS GONEPlease note that the code for this project is now stored in Github http://github.com/piercer/ASAXB . The latest version the source code can only be found there. The latest compiled download will be available here. Are you tired of writing actionscript code that converts objects to XML and back again? If so then this library may be for you.
I recently learnt about the JAXB architecture for java. It enables you to annotate code for marshalling and unmarshalling from Java to XML and back again. You don't actually write the code that does the conversion, instead you give instructions to the marshalling system about how to package your object as XML and it does it for you. These instructions are given via annotations in the Java code. This project is aiming to create a small library for actionscript-3 that performs a similar job to JAXB. It uses metadata to instruct the marshaller/unmarshaller on how to convert between XML and Objects. Even in its current far from complete implementation, it is fairly useful. It currently supports the following metadata - which are equivalent to the JAXB ones. This metadata can currently be applied to public variables and getters/setters. Sample UsageIf you have the following classes And you execute the following code var order:Order = new Order(); var customer:Customer = new Customer(); customer.name = 'Conrad Winchester' customer.address = 'Earth, The Universe'; customer.telephone = '020 2222 2222'; order.customer = customer; order.addItem(5,2.50); order.addItem(2,1.25); var context:ASAXBContext = ASAXBContext.newInstance(Order); var marshaller:Marshaller = context.createMarshaller(); var xml:XML = marshaller.marshal(order); trace(xml.toXMLString()); Then you will see the following output <order>
<customer>
<telephone>020 2222 2222</telephone>
<name>Conrad Winchester</name>
<address>Earth, The Universe</address>
</customer>
<cart>
<items>
<item unit-cost="2.5" quantity="5"/>
<item unit-cost="1.25" quantity="2"/>
</items>
</cart>
</order>Also if you do have the following XML <order>
<customer>
<telephone>020 2222 2222</telephone>
<name>Conrad Winchester</name>
<address>Earth, The Universe</address>
</customer>
<cart>
<items>
<item unit-cost="2.50" quantity="3"/>
<item unit-cost="1.25" quantity="2"/>
</items>
</cart>
</order>and feed it into the unmarshaller, you will get properly poulated objects var context:ASAXBContext = ASAXBContext.newInstance(Order); var unmarshaller:Unmarshaller = context.createUnmarshaller(); var order:Order = unmarshaller.unmarshal(cartXML); trace(order.totalCost); This generate the number 10, which is correct. There are still quite a few things missing from the release, but they will be added soon
I hopw you enjoy this library, and if you wish to get involved, please contact me Conrad Winchester |