Problem with namespaces and array

21 views
Skip to first unread message

battarsa

unread,
Apr 24, 2012, 11:28:03 AM4/24/12
to FxWorks
I found a problem when using a class with a namespace and array
element. Serializing and deserializing a class, the content of the
array is not restored.

Try this example:

file B.as

package
{
[XmlClass]
public class B
{
[XmlAttribute]
public var val:String;

public function B(s:String="")
{
val = s;
}

public function toString():String
{
return "{B val=" + val + "}";
}
}
}

file A.as

package
{
[XmlClass(uri="http://www.test.com/xml/a")]
public class A
{
[XmlArray(alias="*", type="B")]
public var values:Array;

public function A()
{
values = new Array();
}

public function toString():String
{
var s:String = "{A values=[";
for each (var v:B in values) s += v + ", ";
s += "]}";
return s;
}
}
}

file test.mxml

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx">

<fx:Script>
<![CDATA[
import com.googlecode.flexxb.FlexXBEngine;
protected function button1_clickHandler(event:MouseEvent):void
{
// create object structure
var a:A = new A();
a.values.push(new B("help"));
a.values.push(new B("me"));
trace(a);

// serialization
var x:XML;
x = FlexXBEngine.instance.serialize(a);
trace(x.toXMLString());

// convert to string and back to xml like when writing and reading
from file
x = XML(x.toXMLString());

// deserialization
a = FlexXBEngine.instance.deserialize(x, A);
trace(a);
}
]]>
</fx:Script>

<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here
-->
</fx:Declarations>
<s:Button x="97" y="75" label="Button"
click="button1_clickHandler(event)"/>
</s:WindowedApplication>


the first trace output {A values=[{B val=help}, {B val=me}, ]}
the second {A values=[]}

the problem occurs only when these conditions are met:

- the array elements doesn't have an enclosing tag (alias="*")
- the xml have a namespace
- the xml is converted to and back from a string

battarsa

unread,
May 2, 2012, 5:18:00 AM5/2/12
to FxWorks
I found a solution.

The uri (uri="http://www.test.com/xml/a") must be specified for class
B too.

But it's strange that this is needed only in some special case.
Reply all
Reply to author
Forward
0 new messages