问题
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:
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.
回答2:
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.
回答3:
try this also to deserialize xml into Object:
http://ahmadflex.blogspot.com/2010/05/desrializing-helper-class-xml-to-object.html
来源:https://stackoverflow.com/questions/1804441/deserialize-xml-to-custom-class-in-flex