6 Replies Latest reply on Feb 14, 2008 3:15 PM by dazweeja

    Casting XML type to ArrayCollection?

      Hi all,

      I have a HTTPService's resultHandler to do the following:

      if( event.result.list.cluster.drive.source is ArrayCollection )
      ListBoxAC = event.result.list.Region.Market.source;
      ListBoxAC = new ArrayCollection( ArrayUtil.toArray(event.result.list.Region.Market.source) );

      The rest of the function break down and parse the data accordingly. Here is the hard part. I need to provide another alternate XML source. Instead of loading XML data from a HTTPService, I have the data passed from a variable in XML structure. How do I cast the XML data so that my ListBoxAC would has the same structure as using the HTTPService?

      I tried to use the following:

      public function LoadKPIReport(DataXML:XML):void
      ListBoxAC.addItem( ArrayUtil.toArray( DataXML.Region.Market) );

      But, that is trying to make the ArrayCollection in a XMLListCollection. The structure is somewhat different and my pasrer cannot decode. Please help! I wan to re-use the rest of codes to parse different data.
        • 1. Re: Casting XML type to ArrayCollection?
          ntsiii Level 3
          If you have not set the HTTPService resultFormat property, Flex is automatically converting your xml into a structure of nested dynamic Objects. this is rarely desirable, but it does automatically create arrays from XMLLists, which enables you to use dot navigation as you are.

          My advice would be to set resultFormat="e4x", which will give you true xml in your app. But you will have to change how you are using that data. You can use the XML directly, or wrap it in an XMLListCollection. This is much more flexible than the default conversion.

          If you want to stick with the path you are on, you could try using mx:Model to wrap your XML. It performs the same conversion as the resultFormat="object"

          • 2. Re: Casting XML type to ArrayCollection?
            2JZ Level 1

            Is this how I use the Model?
            public function LoadKPIReport(DataXML:XML):void
            ModelCtrl.source = DataXML;
            ListBoxAC = new ArrayCollection();
            ListBoxAC.addItem( ModelCtrl.Region.Market.source );

            <mx:Model id="ModelCtrl"/>

            It doesn't run as I expected. And, I got the following message.
            TypeError: Error #1009: Cannot access a property or method of a null object reference.

            CAn anyone tell me what's wrong with my code?
            • 3. Re: Casting XML type to ArrayCollection?
              What Tracy was saying was that mx:Model parses your XML in the same way as a HTTPService that doesn't have the resultFormat set. What this means is that you don't specify the resultFormat property of mx:Model - it doesn't have this property as it doesn't load the data at run-time, it actually compiles it into the application. So you can either put your XML directly into the model:

              <mx:Model id="ModelCtrl">

              or use:

              <mx:Model id="ModelCtrl" source="fileName" />

              They are both equivalent.

              Then something like your original function should work (hard to tell without seeing your XML):

              public function setKPIReport():void
              ListBoxAC = new ArrayCollection( ArrayUtil.toArray(ModelCtr.Region.Market));

              This is a working example:

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">
              import mx.collections.ArrayCollection;
              import mx.utils.ArrayUtil;
              public function init():void
              var ac:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(MyEmployee.name));
              trace("First name of first object is: " + ac.getItemAt(0).first);
              <mx:Model id="MyEmployee">
              • 4. Re: Casting XML type to ArrayCollection?
                ntsiii Level 3
                Yes, excellent example, thank you dazweja.

                I still advise e4x for the long run.

                • 5. Re: Casting XML type to ArrayCollection?
                  2JZ Level 1
                  Thank you to Tracy & dazweeja. :)
                  • 6. Re: Casting XML type to ArrayCollection?
                    dazweeja Level 1
                    Yes, I agree totally Tracy. If it's already an XML object, why not keep it that way and take advantage of all the great functionality provided by e4x? If you need to add collection functionality to an XML/XMLList object, use XMLListCollection - that's what it's there for. I honestly can't see any good reason to use ArrayCollection over XMLListCollection in situations where your data is an XML object.

                    Here's some good e4x links if anyone's interested: