views:

479

answers:

3

Is it possible to deserialize an XML file to a class in Flex without manually checking the XML and/or creating the class, with the help of a HttpService?

Edit: Explained a bit more and better.

We have an XML file which contains:

<Project>
 <Name>NameGoesHere</Name>
 <Number>15</Number>
</Project>

In Flex we want this to be serialized to our Project class:

package com.examplepackage
{
    import mx.collections.ArrayCollection;

    [XmlClass]
    public class Project
    {
     public var Name:String;
     public var Number:int; 

     public function Project()
     {
     }
    }
}

The XML is loaded with a HTTPService.

+1  A: 

In order to have Flex automatically serialize and deserialize objects for your, there needs to be some kind of WSDL or RPC Protocol. So you either need to specify a WSDL to use to deserialize XML data, use AMF or some other protocol, or work with whatever MIME type your httpservice returns directly.

EDIT: You could set up your own set of classes to handle the deserialization of your xml objects. The way the AMF service works is it first deserialzes the returned objects into mx.utils.ObjecProxy instances, then matches the type attribute of the ObjectProxy to an existing RemoteClass and initializes the properties of the class in the PropertyList to values in the dynamic Object property. You could set your own set of classes that create ObjectProxy's by looking at the XML root for the type and child nodes for the properties and values, then instantiate your AS classes based on the Object proxies. You aren't going to be able to get around having to write some code to directly handle the XML though.

Ryan Lynch
Hmm I see. Writing our own set of classes to handle the serialization/deserialization isn't really an option. So I think we're have no other option than doing it manually then. Thanks!
MysticEarth
You can see what I mean though about needing some kind of definition to map the values from the XML object to an AS class. For example, without looking at a Schema, the `<Number>` node is read as a string value, and the `Number` property of your `Project` class is typed as an `int`. You need some kind of schema or additional information to handle the mapping, which is why Flex supports AMF and WSDL based web services.
Ryan Lynch
A: 

You may want to try out an xml serialization library such as FlexXB (http://code.google.com/p/flexxb). It uses annotations to automate the (de)serialization process allowing you a good control over the resulting xml.

Alex
A: 

try this also to deserialize xml into Object:

http://ahmadflex.blogspot.com/2010/05/desrializing-helper-class-xml-to-object.html

Ahmad