views:

6562

answers:

4

In Flex, it's easy to convert the XML to Object and to ArrayCollection by using var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(); decoder.decodeXML( xml );

But is there a good way to convert ArrayCollection to XML. I have search a while on the internet and havn't found any answer.

Thanks.

+1  A: 

Why not convert the array returned by toArray() method of the ArrayCollection, with SimpleXMLEncoder?

bug-a-lot
+2  A: 

Have a look at this example, which uses SimpleXMLEncoder:

<mx:Script>
 <![CDATA[

  import mx.rpc.xml.SimpleXMLEncoder;
  import mx.utils.ObjectUtil;
  import mx.utils.XMLUtil;
  import mx.collections.ArrayCollection;

  private var items:ArrayCollection;

  private function onCreationComplete():void
  {
   var source:Array = [{id:1, name:"One"}, {id:2, name:"Two"}, {id:3, name:"Three"}];
   var collection = new ArrayCollection(source);

   trace(objectToXML(collection.source).toXMLString());
  }

  private function objectToXML(obj:Object):XML 
  {
   var qName:QName = new QName("root");
   var xmlDocument:XMLDocument = new XMLDocument();
   var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
   var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
   var xml:XML = new XML(xmlDocument.toString());

   return xml;
  }

 ]]>
</mx:Script>

...which produces the following XML:

<root>
  <item>
    <id>1</id>
    <name>One</name>
  </item>
  <item>
    <id>2</id>
    <name>Two</name>
  </item>
  <item>
    <id>3</id>
    <name>Three</name>
  </item>
</root>

I should note that got the objectToXML function from Peter de Haan's blog, but folks have apparently had a few problems with the SimpleXMLEncoder class. In my own tests, encoding simple objects works well (as indicated above), but complex types tend to produce unpredictable results. (For example, encoding an array of Font objects produced a long list of empty item nodes.)

But depending on the types you're attempting to serialize, this approach might work out just fine for you. Hope it helps!

Christian Nunciato
I would hardly call Font a complex type. The problem with that class is that it has 1 public property that is ignored (constructor) and 3 read-only ones. I'm pretty sure read-only properties are ignored by the serializer.
bug-a-lot
Maybe I should've put "complex" in quotes, then. ;) Whatever you choose to call it, it didn't work, so I thought that should be pointed out.
Christian Nunciato
Thanks a lot. The SimpleXMLEncoder good enough to convert an ArrayCollection of Objects to XML
maoanz
how can I add nested nodes? My XML structure needs to have 3 child nodes, per parent. Any ideas?
Devtron
specifically, how can I change "item" to be named something else? Like "account"?
Devtron
A: 

You could try using the descibeType() method that takes in an object and converts it to xml - however i think this just converts the class outline? Might be a start though.

Jon
A: 

Nice code snippet.

Much useful for developers !

Thanks, Nith

Nith