XML to VO – as easy as apple pie!? (demo included)

I’m currently working with SOAP web services. While making adjustments to the classes of the wsdl Flex Builder class generator I discovered, that there is an easy way to decode / parse XML to VOs (value objects). The XMLDecoder inside the rpc package does the trick. I’m still confused why there are not more resources on the web regarding this topic but If you want to digg deeper, I strongly recommend reading these blog posts by Dominic De Lorenzo:
Flex 3, XML Schemas & automatic mapping of AS classes to XSD element definitions (Part 1)
Flex 3, XML Schemas & automatic mapping of AS classes to XSD element definitions (Part 2)
I’m not an XML pro and some things in his example I’m still not getting (namespaces in XML, yes well…). Also, his example is in Flex and I just wanted to have a basic example on how this could be useful for my work.
Example / Demo
You have to tell the XMLDecoder what XML node is assigend to which VO type. “Schema” is the magic word – so before feeding the actual XML data to the XMLDecoder the SchemaManager needs to know what the XML structure looks like. After that, you just have to map the schema types to your custom VOs.
You can take a look at the VO classes here:
FieldVo
GroupVo
And this’s the actual example implementation …
private var schemaXML:XML =
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="root" >
<xsd:complexType >
<xsd:sequence >
<xsd:element minOccurs="0" maxOccurs="unbounded" name="group" type="group"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:complexType name="group">
<xsd:sequence>
<xsd:element name="id" type="xsd:string" />
<xsd:element name="field" type="field" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="group" type="group" minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType>
<xsd:complexType name="field">
<xsd:sequence>
<xsd:element name="id" type="xsd:string" />
<xsd:element name="index" type="xsd:int" />
<xsd:element name="label" type="xsd:string" />
<xsd:element name="visible" type="xsd:boolean" />
</xsd:sequence>
</xsd:complexType>
</xsd:schema>;
private var dataXML:XML =
<root>
<group>
<id>group01</id>
<field>
<id>field01</id>
<index>1</index>
<label>test field 01</label>
<visible>1</visible>
</field>
<field>
<id>field02</id>
<index>2</index>
<label>test field 02</label>
<visible>0</visible>
</field>
<field>
<id>field03</id>
<index>3</index>
<label>test field 03</label>
<visible>1</visible>
</field>
<group>
<id>group0101</id>
<field>
<id>field04</id>
<index>4</index>
<label>test field 04</label>
<visible>1</visible>
</field>
<field>
<id>field05</id>
<index>5</index>
<label>test field 05</label>
<visible>0</visible>
</field>
</group>
</group>
<group>
<id>group02</id>
<field>
<id>field06</id>
<index>6</index>
<label>test field 06</label>
<visible>1</visible>
</field>
</group>
</root>;
var schema:Schema = new Schema(schemaXML);
var schemaManager:SchemaManager = new SchemaManager();
schemaManager.addSchema(schema);
var schemaTypeRegistry:SchemaTypeRegistry = SchemaTypeRegistry.getInstance();
schemaTypeRegistry.registerClass(new QName(schema.targetNamespace.uri, "group"), GroupVo);
schemaTypeRegistry.registerClass(new QName(schema.targetNamespace.uri, "field"), FieldVo);
var qName:QName = new QName(schema.targetNamespace.uri, "root");
var xmlDecoder:XMLDecoder = new XMLDecoder();
xmlDecoder.schemaManager = schemaManager;
var result:* = xmlDecoder.decode(dataXML, qName);
trace(result);
You might want to set a breakpoint at the trace statement so you can click through the result object step by step.
Please contribute!
Did you ever work with the XMLDecoder? Do you think, that this might improve your workflow? Just tell us.

Very interesting post. Question, why not just pass around raw XML and e4x it what you need. It looks like you have to go through hoops to define the schema in the first place just so you have a method of parsing it? Another method would be to have a setter on a VO to take in XML and then have getters access the paths.
My dream solution would be define the entity on the sever-side and in flex, it would just code-complete a reference to that entity’s interface with the least amount of work as possible.
Sure, if you’re fine with straight XML, why decode / parse in the first place? However, some developers (us for instance ;) ) prefer working with VO’s.
The schema stuff centralize the definition of your XML. When decoding the XML inside the VO (we also did this in the past) you might end up with lots of classes to change, when there is a change in the XML schema.
That looks interesting… but the same result can be achieved without defining the schema at all using the class detailed here:
http://dispatchevent.org/roger/instant-model-binding-with-reflection/
Addendum: point taken about the advantages of using a schema though.
Hi Oliver, thanks for the info. I just tested that with the example xml above and it seams that the script does not work with nested vo’s.
In the example above, the XMLDecoder decodes the nested vo types GroupVo and FieldVo like a charm.
The code works only by replacing the lines 88-89 with the following:
schemaTypeRegistry.registerClass(new QName(schema.targetNamespace.uri, “group”), GroupVo);
schemaTypeRegistry.registerClass(new QName(schema.targetNamespace.uri, “field”), FieldVo);
XML element names and VO names are incorrect. Except for this minor typo, your code works great!!
Thanks Nav for letting us know. I just fixed that.
It’s a shame that it stand there for so long, without anyone noticing it… ;)
Very useful. How would you go about parsing an xml attribute to a vo variable?
Man, you saved my life!!!! You and Mr. Dominic De Lorenzo. I catch up my webservice WSDL, create an Schema object with his types and the magic was done, I also did the encoding process too and works ok, beacuse I had to send an request message with an complex type inside.
Just plus one information:
To get the Flex XML classes of mx.rpc.xml.*, go to /x.x.x/frameworks/projects/rpc/src
I recomend other links very interesting, to understang WSDL and XML Schema Hierarchy:
http://www.predic8.com/wsdl-reading.htm – About WSDL structure by Thomas Bayer
http://oreilly.com/catalog/webservess/chapter/ch06.html – About webservice structure By O’Reilly
http://www.xml.com/pub/a/2001/08/22/easyschema.html – This makes me understand the complex types in a better approach By Donald Smith
http://www.w3schools.com/schema/schema_schema.asp – Well, the basis By W3C
Thank you again!!!!!!!!
Thanks for the example :)
We’re using this to export/import data for a simple app.
Cool stuff!