10 Replies Latest reply on Oct 22, 2008 1:31 PM by SiHoop

    XML data to ArrayCollection

    SiHoop Level 1
      How do I load xml data into an ArrayCollection? I thought the following line would do it, but it fails:
      people = myXml.employee as ArrayCollection;
        • 1. Re: XML data to ArrayCollection
          Level 7

          "SiHoop" <webforumsuser@macromedia.com> wrote in message
          news:gdllo0$qf5$1@forums.macromedia.com...
          > How do I load xml data into an ArrayCollection? I thought the following
          > line
          > would do it, but it fails:
          > people = myXml.employee as ArrayCollection;

          I would think this would be a really bad idea, since each node of XML
          contains a reference to the entire XML structure. What's your objective?

          You could try:
          tmpXMLListCollection:XMLListCollection = new
          XMLListCollection(myXml.employee);
          tmpArray:Array = tmpXMLListCollection.toArray();
          people = new ArrayCollection(tmpArray);

          HTH;

          Amy


          • 3. Re: XML data to ArrayCollection
            ntsiii Level 3
            No, if you have XML then use XMLListCollection instead.

            If you really need an ArrayCollection you will need to manually loop over the XMLList and build the collection.

            Tracy
            • 4. Re: XML data to ArrayCollection
              SiHoop Level 1
              What I'd like to be able to do is update the xml which I've found easy with an ArrayCollection. The code below works to display the xml, but I can't get the XMLListCollection to update in the same way.
              • 5. Re: XML data to ArrayCollection
                Level 7

                "SiHoop" <webforumsuser@macromedia.com> wrote in message
                news:gdlpes$1uk$1@forums.macromedia.com...
                > What I'd like to be able to do is update the xml which I've found easy
                > with an
                > ArrayCollection. The code below works to display the xml, but I can't get
                > the
                > XMLListCollection to update in the same way.
                >
                > <?xml version="1.0" encoding="utf-8"?>
                > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
                > creationComplete="init();">
                > <mx:XMLListCollection id="xmlList" source="{myXml.employee}" />
                > <mx:Panel width="100%">
                > <mx:DataGrid dataProvider="{xmlList}">
                > <mx:columns>
                > <mx:DataGridColumn width="100" dataField="name" />
                > </mx:columns>
                > </mx:DataGrid>
                > </mx:Panel>
                > <mx:Button label="Add" click="xmlList.addItem({name:'Sally'});"/>
                > <mx:Script>
                > <![CDATA[
                > import mx.collections.XMLListCollection
                > [Bindable] private var people:XMLListCollection ;
                > [Bindable] private var myXml:XML;
                > private function init():void{
                > myXml =
                > <employees>
                > <employee>
                > <name>Fred</name>
                > </employee>
                > <employee>
                > <name>Andy</name>
                > </employee>
                > </employees>;
                > }
                > ]]>
                > </mx:Script>
                > </mx:Application>

                Try adding an entire node ;-)


                • 6. Re: XML data to ArrayCollection
                  SiHoop Level 1
                  Do you mean that xmlList.addItem({name:'Sally'}) is incomplete?
                  • 7. Re: XML data to ArrayCollection
                    Level 7

                    "SiHoop" <webforumsuser@macromedia.com> wrote in message
                    news:gdlrae$4a0$1@forums.macromedia.com...
                    > Do you mean that xmlList.addItem({name:'Sally'}) is incomplete?

                    Where would you expect that to fit into your existing XML?


                    • 8. Re: XML data to ArrayCollection
                      SiHoop Level 1
                      OK, I see what you mean. I have a solution in place, but there is one line that is really ugly:
                      <employee><name>{"Sally"}</name></employee>
                      I can replace "Sally" with a variable, but what about <employee><name>? Do I have to use parent/child relationships to make that more elegant?
                      Thanks for leading me through that process-- I learned a lot from figuring it out!
                      • 9. Re: XML data to ArrayCollection
                        Level 7

                        "SiHoop" <webforumsuser@macromedia.com> wrote in message
                        news:gdm020$a5t$1@forums.macromedia.com...
                        > OK, I see what you mean. I have a solution in place, but there is one line
                        > that
                        > is really ugly:
                        > <employee><name>{"Sally"}</name></employee>
                        > I can replace "Sally" with a variable, but what about <employee><name>? Do
                        > I
                        > have to use parent/child relationships to make that more elegant?
                        > Thanks for leading me through that process-- I learned a lot from figuring
                        > it
                        > out!
                        >
                        > <?xml version="1.0" encoding="utf-8"?>
                        > <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml"
                        > creationComplete="init();">
                        > <mx:Panel width="100%">
                        > <mx:DataGrid dataProvider="{myXmlList}">
                        > <mx:columns>
                        > <mx:DataGridColumn width="100" dataField="name" />
                        > </mx:columns>
                        > </mx:DataGrid>
                        > </mx:Panel>
                        > <mx:Button label="Add" click="addXml()"/>
                        > <mx:Script>
                        > <![CDATA[
                        > import mx.collections.XMLListCollection
                        > [Bindable] private var myXmlList:XMLListCollection ;
                        > [Bindable] private var myXml:XML;
                        > private function init():void{
                        > myXml =
                        > <employees>
                        > <employee>
                        > <name>Fred</name>
                        > </employee>
                        > <employee>
                        > <name>Andy</name>
                        > </employee>
                        > </employees>;
                        > myXmlList=new XMLListCollection
                        > myXmlList.source=myXml.employee
                        > }
                        > private function addXml():void{
                        > var newPerson:XML =
                        > <employee><name>{"Sally"}</name></employee>;
                        > myXmlList.addItem(newPerson);
                        > }
                        > ]]>
                        > </mx:Script>
                        > </mx:Application>

                        Personally, I wouldn't add an item directly into an XMLListCollection, just
                        because it is possible to create an XMLList as its source that takes
                        elements from multiple levels of the XML. I don't know how the collection
                        would know where to insert the new item. Instead, I'd add the item to the
                        exact location in the XML where I wanted it to end up.

                        So, something like this:

                        var newNode:XML = <employee></employee>
                        newNode.name='Sally';

                        myXml.appendChild(newNode);

                        I'm not sure if the best approach at that point is to call

                        myXmlList.itemUpdated(newNode);

                        or just reset the source or refresh the collection.

                        Tracy probably has a different opinion. I'll defer to him as our resident
                        XML guru.


                        • 10. Re: XML data to ArrayCollection
                          SiHoop Level 1
                          The code below is used to put xml into a datagrid when the xml is stored inside the mxml file. It works, but what I really want to do is understand how to put that xml into an ArrayCollection (instead of using XMLListCollection). In my full application, I will be pulling xml from a database. It is straightforward to put xml from a database into an ArrayCollection, so I assume that it must be simple to put the data into an ArrayCollection when the xml is stored internally. The commented out lines in the code below just illustrate what I would be doing when pulling from the database. So, is there a simple way to put the internally stored xml into an ArrayCollection