5 Replies Latest reply on Jul 6, 2010 9:08 PM by reg288

    Retrieving XML data from the Server - Collection of one item

    AGVernon

      For the abovementioned tutorial, instread of downloading the resources, I tried supplying my own XML file. This file contained only one employee

       

      <?xml version="1.0" encoding="utf-8"?> 
      <employees> 
         <employee> 
           <firstName>Arthur</firstName> 
           <lastName>Vernon</lastName> 
        </employee> 
      </employees> 

      I tried this wth just one employee and it failed.  To be sure, I added a second employee and it works (the drop down list was populated with employee surnames).

       
      Logically I would have thought that the component should have worked with zero, one or many elements.

       

      Has anyone else experienced this kind of behaviour (error message appearing complaining about not being able to cast an object to a collection)

       

      As this is a real possibility, is this a bug? Have I done something wrong?

        • 1. Re: Retrieving XML data from the Server - Collection of one item
          therese8 Level 1

          I will look into this and get back to you.


          • 2. Re: Retrieving XML data from the Server - Collection of one item
            Flex in a Week, FiaW Level 1

            Check the Help to see if DropDownList  controls are supposed to be used if there is only one data object being returned.  You would likely need more than one option available for a DropDownList.

            • 3. Re: Retrieving XML data from the Server - Collection of one item
              FlexualHealing

              Hi AGVernon,

               

                      The problem you are having is caused by the way the ArrayCollection class is used to handle the data being returned from the XML. The ArrayCollection class is only used when it NEEDS to be used, which Flex considers an XML file with 2 or more data objects in it. This is why it works with two employees vs. one.

               

              When Flex sees there is only one object in the XML, it doesn't consider this as NEEDING the ArrayCollection class, Flex defaults the data object to the ObjectProxy class.

               

              If there were no objects in the XML file, you would recieve an error saying the return type was "null".

               

              The workaround:

               

                 Since this is not a "bug" so to speak, you should consider using a conditional and the ArrayUtil.toArray() method to handle a single result vs. multiple results.  The ArrayUtil.toArray() method will convert the ObjectProxy class to the Array class and thus allow it to be handled by the ArrayCollection class.

               

              P.S. dont forget to import the ArrayUtil package

               

              ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

               

              [Bindable]

              public var employees:ArrayCollection = new ArrayCollection();

               

              employeesXMLHTTPService_resultHandlerFunction(event.ResultEvent)

              {


                   if (event.result.employees.employee is ObjectProxy)

               

                        employees = new ArrayCollection( ArrayUtil.toArray(event.result.employees.employee) );

               

                   else

               

                      employees = event.result.employees.employee;

              }

              • 4. Re: Retrieving XML data from the Server - Collection of one item
                AGVernon Level 1

                The API documentation for combo box states for:

                • dataProvider - "Set of data to be viewed. Some classes use a simple array. Others use XML heirarchies. Still others use a class instance. In general, do not make assumptions about the composition of a dataProvider."
                • length - "Number of items in the dataProvider. Note that this is not necessarily the number of items displayed."

                So I am none the wiser.

                I would have thought that a list of one would be acceptable. For now I also assume that would be the case.

                 

                In relation to the comment

                "When Flex sees there is only one object in the XML, it doesn't consider this as NEEDING the ArrayCollection class, Flex defaults the data object to the ObjectProxy class." ...

                I can appreciate this from an implementation perspective, merely relying on the data presented, but I also think it is important to consider the context in which that data is presented, i.e. the client WANTING a collection, even if it is a collection of one (in this case) or zero.

                 

                I suspect that the data provider is unaware of the context and hence there is no solution except for the client in such cases to handle the possibility of an empty or single-item collection and respond with alternative GUI components.

                 

                The consequences are that instead of a "simple" declaration, robust code would need to (psuedo code):

                if empty collection

                     handle empty collection with some component

                else if single item collection

                    maybe convert into array collection and render in a combo box or just render it in a text field

                else

                    render it in a combo box as planned.

                • 5. Re: Retrieving XML data from the Server - Collection of one item
                  reg288

                  I have the same problem as you