6 Replies Latest reply on Jul 11, 2008 10:15 AM by Gregory Lafrance

    Converting an XML File to an Array Collection

    ericbelair Level 1
      I recently downloaded a sample Flex application from an Adobe employee as a means of teaching myself how to use Cairngorm. In the sample application, there is an HTTPService that accesses an XML file in the application's directory structure (/src/assets/photos.xml). In the result handler, the result is converted to an Array Collection (see attached code). I never knew this could be done, but it somehow works. I tried to duplicate this for a new application I am writing useing Cairngorm. The HTTPService works, and the result comes back, but it cannot be converted to an ArrayCollection. I get the following Runtime Error:

      TypeError: Error #1034: Type Coercion failed: cannot convert mx.utils::ObjectProxy@4d39749 to mx.collections.ArrayCollection.
      at control.commands::LoadAccountsCommand/resultHandler()[C:\Documents and Settings\ebelair\My Documents\Flex Builder 3\Budget\src\control\commands\LoadAccountsCommand.as:24]

      I ran both applications in Debug Mode, and noticed one difference, which makes no sense to me. I put a breakpoint on the first line of the result handler, and int he Variables window, the event.result looks like this in the sample application:

      event
      ---result
      ------object
      ---------photos
      ------------image
      ---------------(more nested levels)
      ------------object
      ---------------(more nested levels)
      ------photos
      ---------image
      ------------(more nested levels)
      ---------object
      ------------(more nested levels)

      and like this in my application:

      event
      ---result
      ------accounts
      ---------account
      ------------ (more nested levels)
      ---------object
      ------------account
      ---------------(more nested levels)
      ------object
      ---------accounts
      ------------account
      ---------------(more nested levels)
      ------------object
      ---------------(more nested levels)

      Why is there that added "object", and why is it ordered differently in each application?

      I know I can just try to use an XMLListCollection, but I not looking for a workaround, I'm looking for an explanation of how this works/why it works this way.

      Thanks.
        • 1. Re: Converting an XML File to an Array Collection
          ericbelair Level 1
          Flex drives me crazy. I recompiled my application at least 30 times yesterday trying to get this to work, and just now, I recompile, and it works. WTH?!
          • 2. Re: Converting an XML File to an Array Collection
            ericbelair Level 1
            Now having the same issue in another application - a non-Cairngorm application. I don't get this. The XML is fine, because I was able to import it with an <mx:XML> tag with no RTE. Someone please help....
            • 3. Re: Converting an XML File to an Array Collection
              ericbelair Level 1
              Well I figured out what might be the problem...

              My XML originally only had one <account> node. It started working after I added a second <account> node. I guess it doesn't see it as an Array, unless there is more than one object at the root of what you're trying to set as an ArrayCollection???
              • 4. Re: Converting an XML File to an Array Collection
                ntsiii Level 3
                This is one of the problems with leaving resultFormat at the default "object" Because Array is an object, the player cannot differentiate between and Array with a single element and an Object. There are workarounds that examine the dataType in the resutl and act accordingly, also wrapping the object in ArrayUtil.toArray()

                But that is all crap. Set resultFormat="e4x". In the handler, do:
                var xmlResult:XML = XML(event.result);
                trace(xmlResult.toXMLString()); //see your xml? Yaay!

                Now, you have a choice. You can stay in the xml world, using the XML directly or with XMLList or XMLListCollection. I use this mostly.

                If you will be displaying large amounts of data in many visible complex item renderers, consider building your own ArrayCollection. Accessing values of strongly typed objects is significantly faster thatn accessing values from XML nodes. Loop through your xml and populate a strongly typed value object with each node's data. Push this onto an Array. When done, wrap the Array in an Array collection.

                Tracy

                • 5. Re: Converting an XML File to an Array Collection
                  smonika15
                  Thanks ntsiii

                  I also face dthe same problem in my application and I used mx.utils.ArrayUtil.toArray() function only.
                  can you please more elaborate that setresultFormat stuff?
                  better to give an example if possible.

                  Thanks
                  Monika



                  • 6. Re: Converting an XML File to an Array Collection
                    Gregory Lafrance Level 6
                    resultFormat is a property of your HTTPService object:

                    var service:HTTPService = new HTTPService();
                    service.resultFormat = "e4x"; <- SEE THIS LINE
                    service.url = "assets/hierarchy.xml";
                    service.addEventListener(ResultEvent.RESULT, resultHandler);
                    service.send();