0 Replies Latest reply on Oct 28, 2010 8:58 PM by thePlatypus

    Export ArrayCollection to XML

    thePlatypus

       

      I’m a little confused onhow to convert my ArrayCollection containing a custom class (actionscriptimport) in each node into an xml.

       

       

      My custom class is VERY simple…

       

      public class Widget

      {                                              

           public var name:String //typically I would make this private but I got no results without publicizing it   

           public function Widget()

           {

                 //constructor

           }

           public function SetName(_name:String):void {

                name = _name;

            }

            public function GetName():String {

                return name

            }

      }

       

       

      I create a bunch of these in an ArrayCollection using a simple form to feed in the data, then I want toexport the xml. I found the following code and tried to apply it…

       

       

      private function Convert_Click():void{

           arrayCollectionToXML(myAls);

      }                                                                                        

       

      private functionarrayCollectionToXML(ac:ArrayCollection):void{

           varxml:XML

           for(var key:Object in myAls) {

                xml= objectToXML(myAls[key]);

                display.text+= xml;

           }

      }         

       

      private functionobjectToXML(obj:Object):XML {

                               varqName:QName = new QName("root");

                                    varxmlDocument:XMLDocument = new XMLDocument();

                                    varsimpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);

                                    varxmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);

                                    varxml:XML = new XML(xmlDocument.toString());

                                    returnxml;

                               }

       

      With my code as is when I input some widgets and assign names I get…

       

      <root><name>junk</name></root><root><name>junk1</name></root><root><name>junk2</name></roo t>

       

      I understand that each time I make a call to my function objectToXML() I am essentially stating to make another xml file, but I'm not sure how else to get xml output for my objects and their properties. When I directly pass theArrayCollection I get a breakdown on the object itself. I’ve seen where otherexamples usually piece their data together using arrays then converting them toan ArrayCollection.

       

       

      My question is… what am I missingto make this work the way I expect it? I’m tempted to simply iterate through myArrayCollection and concatenate a giant string.