5 Replies Latest reply on Nov 6, 2009 6:16 AM by SiHoop

    Problem importing xml to an ArrayCollection

    SiHoop Level 1

      If I import the following xml file into the mxml file below, everything works. However, if I delete one or both of the <details> tags, an error is produced. Why can't I import the data into the arrayCollection when less than two children exist? Is there a way to resolve the problem?

       

      data.xml

      <employees>
              <details>
                       <id>1001</id>
                       <name>A</name>
              </details>
              <details>
                       <id>1002</id>
                       <name>S</name>
                </details>
      </employees>

       

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" initialize="service1.send()" >
          <mx:Script>
              <![CDATA[
                  import mx.collections.ArrayCollection;
                  import mx.rpc.events.ResultEvent;
                  private var arrayCollection:ArrayCollection=new ArrayCollection;
                  private function serviceLoaded(e:ResultEvent):void{    
                      arrayCollection=e.result.employees.details as ArrayCollection;
                      dataGrid.dataProvider=arrayCollection;
                      trace("arrayCollection.length="+arrayCollection.length)
                  }
              ]]>
          </mx:Script>
          <mx:HTTPService id="service1" url="data.xml" result="serviceLoaded(event)"/>
          <mx:DataGrid id="dataGrid"/>  
      </mx:Application>

        • 2. Re: Problem importing xml to an ArrayCollection
          iamfuric Level 2

          Simple :

            <![CDATA[
                      import mx.collections.ArrayCollection;
                      import mx.rpc.events.ResultEvent;
                      private function serviceLoaded(e:ResultEvent):void{    
                          dataGrid.dataProvider=XML(e.result).details;
                     }
                  ]]>

           

          Oops, your problem  seems to be to get ArrayCollection, not just assing tables' dataProvider...

          Can you convert XMLList (which is XML(e.result).details) to ArrayCollection ?

          • 3. Re: Problem importing xml to an ArrayCollection
            SiHoop Level 1

            I found the following solution, but I don't understand it! The solution involves the use of an ObjectProxy. Apparently, if my xml contains only a single element, I have to treat it as an ObjectProxy and convert the incoming array to an ArrayCollection. Why is this so? Why can't I import my single element straight into an ArrayCollection? Is it a problem with the way the ArrayCollection class was constructed?

             

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical" initialize="service1.send()" >
                <mx:Script>
                    <![CDATA[
                        import mx.utils.ObjectProxy;
                        import mx.collections.ArrayCollection;
                        import mx.rpc.events.ResultEvent;
                         [Bindable] private var arrayCollection:ArrayCollection= new ArrayCollection
                        private function serviceLoaded(e:ResultEvent):void{   
                            if( e.result.employees.details == null ) {
                                arrayCollection = new ArrayCollection
                            }else if(e.result.employees.details is ObjectProxy ) {
                                arrayCollection = new ArrayCollection([e.result.employees.details]);
                            }else {
                                arrayCollection = ArrayCollection(e.result.employees.details);
                            }
                            dataGrid.dataProvider=arrayCollection;
                            trace("arrayCollection.length="+arrayCollection.length)
                        }
                    ]]>
                </mx:Script>
                <mx:HTTPService id="service1" url="data.xml" result="serviceLoaded(event)"/>
                <mx:DataGrid id="dataGrid"/> 
            </mx:Application>

            • 4. Re: Problem importing xml to an ArrayCollection
              Flex harUI Adobe Employee

              I tried to explain it in that blog post I referenced.  I think your solution is correct.  Did you consider using resultFormat="e4x" and XMLListCollection instead?

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

              Blog: http://blogs.adobe.com/aharui

              • 5. Re: Problem importing xml to an ArrayCollection
                SiHoop Level 1

                I did try using both an XMLListCollection and e4x. Everything worked until I tried editing an item in the XMLListCollection. For some reason Flex changed the XMLListCollection to an Object and I could not re-cast the Object back to an XMLListCollection. I spent a tremendous amount of time trying to figure out what was going on and in the end reverted to ArrayCollection because it worked!

                 

                So, the bottom line is that my code works, but I really don't understand it!! Thanks for helping out.