Andres,
Your problems are actually two-fold, I believe.
The first is that SimpleXMLEncoder and SimpleXMLDecoder do not appear to be
symmetrical with Array properties.
If your object has a property
var arrayProperty:Array
The SimpleXMLEncoder writes
<arrayProperty>
<item>val1</item>
<item>val2</item>
<property>
But when SimpleXMLDecoder decodes this XML it produces an object with
arrayProperty = { item: [val1, val2] }
This is becase SimpleXMLDecoder tries to be "smart" and sees the two <item>
tags and decides that the implied array you want is actually a property
called "item".
I regard this as a bug in Flex 3 - encode/decode should be symmetrical.
Your second problem is that you can't just cast an arbitrary object to being
of a specified type. Whether the fields line up or not doesn't matter: you
simply can't cast this way.
Andres Serral wrote:
>
> Hello.
> I want to convert my object "MyVO" into a XML. for this I use the
> SimpleXmlEncoder class
> then I want to decode this xml into a new object MyVO class using the
> SimpleXmlDecoder class <- Here fails
> I think that it isn“t work because my MyVo contains a Array.
>
> public class MyVO {
> public var id : int;
> public var name : String;
> public var childs : Array;
> }
>
>
> here is a code examplo:
>
> // first create MyVO for example
> var myObj : MyVO = new MyVO();
> myObj.id = 1;
> myObj.name = "pppppp";
> myObj.childs = new Array();
> myObj.childs.push(new MyVO());
> myObj.childs.push(new MyVO());
>
> // now Encode MyVO into XML
> var qName:QName = new QName("root");
> var xmlDocument:XMLDocument = new XMLDocument();
> var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
> var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(myObj, qName,
> xmlDocument);
>
> // now decode the XML
> var decoder:SimpleXMLDecoder = new SimpleXMLDecoder();
> var myObj : Object = decoder.decodeXML(XMLNode(xmlDocument.firstChild));
>
> // finally cast the object to MyVO
> var newVO : MyVO = obj as MyVO; // this dont work
>
> // at this point newVO is null
> // take attention on myObj var see the "childs" property it must to be an
> Array and is a Object type
>
>
> Thanks
>
> Andres
>
>
>
--
View this message in context:
http://www.nabble.com/SimpleXmlEncoder-SimpleXmlDecoder-problem-tp18535572p19688811.html
Sent from the FlexCoders mailing list archive at Nabble.com.