2 Replies Latest reply on Oct 23, 2009 11:29 AM by SiHoop

    Problem importing xml

    SiHoop Level 1

      The following mxml code imports an xml file and reports the length of the ArrayCollection into which the xml is loaded. If the xml file contains only a single <distractor> element I get this error message:

      TypeError: Error #1034: Type Coercion failed: cannot convert mx.utils::ObjectProxy@1d865821 to mx.collections.ArrayCollection.

       

      If the main xml node contains more than one <distractor> element, it works. Is there a problem with the way the xml is written or is there something else I'm doing wrong?

      Here's the xml. The file name is temp.xml:

       

      <?xml version="1.0" encoding="utf-8" standalone="no" ?>
      <distractorsList>
          <distractor>
              <testData>AAA</testData>
          </distractor>
          <distractor>
              <testData>BBB</testData>
          </distractor>
      </distractorsList>

       

      Here's the mxml code:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
          <mx:Script>
              <![CDATA[
          import mx.collections.ArrayCollection;
          import mx.rpc.events.ResultEvent;
          import mx.rpc.http.HTTPService;
      private function init():void{
          var service:HTTPService = new HTTPService();
          service.url = "temp.xml"
          service.method = "GET";
          service.addEventListener("result", recordsReturned);
          service.send()
      }
      private function recordsReturned(event:ResultEvent):void{
          var arrayCollection:ArrayCollection=event.result.distractorsList.distractor
          trace("xx="+arrayCollection.length)
      }
              ]]>
          </mx:Script>
      </mx:Application>

        • 1. Re: Problem importing xml
          camfieldaj

          add service.resultFormat="e4x";

           

          and reference the arrayCollection with

           

          var arrayCollection:ArrayCollection=event.lastResult.distractor;

           

          I think that returns an XMLListCollectiion, however, so if you're determined

          to put it into an arrayCollection, you'll have to cast it as such.

          • 2. Re: Problem importing xml
            SiHoop Level 1

            I tried your recommendation with a minor modification (i.e. I used result instaed of lastResult) with each of the following (see the commented lines in  function recordsReturned), but none worked. Is there something else that I'm missing?

             

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
                <mx:Script>
                    <![CDATA[
                import mx.collections.ArrayCollection;
                import mx.rpc.events.ResultEvent;
                import mx.rpc.http.HTTPService;
            private function init():void{
                var service:HTTPService = new HTTPService();
                service.resultFormat="e4x"
                service.url = "temp.xml"
                service.method = "GET";
                service.addEventListener("result", recordsReturned);
                service.send()
            }
            private function recordsReturned(event:ResultEvent):void{
                //var arrayCollection:ArrayCollection=event.result.distractorsList as ArrayCollection;
                //var arrayCollection:ArrayCollection=event.result.distractor as ArrayCollection;
                var arrayCollection:ArrayCollection=event.result.distractorsList.distractor as ArrayCollection;
                trace("arrayCollection="+arrayCollection.length)
            }
                    ]]>
                </mx:Script>