views:

1037

answers:

2

Hi guys.

Yes there is a question like this one, but there is no activity and no answer.

I want to load data from a external XML file, using a HTTPService, and on the ResultEvent of the same HTTPService i want him to populate a ArrayCollection with the data from the XML.

I think a ArrayCollection is the ideal for this XML. but I'm open to suggestions.

XML

<?xml version="1.0" encoding="utf-8"?>
<PhotoGalleryData>
    <Photo>
     <id>1</id>
     <name>Summer Vacation</name>
     <description>In vacation</description>
     <source>vacation.JPG</source>
    </Photo>
    <Photo>
     <id>2</id>
     <name>Winter Vacation</name>
     <description>coold</description>
     <source>vacation2.JPG</source>
    </Photo>
</PhotoGalleryData>

I thought that this simple line in getDataResultHandler(), would be enough to populate the ArrayCollection.

<mx:HTTPService id="getData"
     url="{XMLDataFileLocation}"
     fault="getDataFaultHandler()"
     result="getDataResultHandler()"/>

[Bindable]
private var PhotoData:ArrayCollection;

private function getDataResultHandler():void
{
    PhotoData = new ArrayCollection(getData.lastResult.PhotoGalleryData.photo)
}

But i guess it isn't, because just to be sure i have placed a List binded to the ArrayCollection to actually see if it really was populated.

<mx:List dataProvider="{PhotoData}" labelField="name"/>

And the list didn't showed any data, so isn't working as supposed to be.

Thanks for any help.

EDIT

NOTE

The <mx:List/> used is just to be sure that the ArrayCollection is indeed populated, it won't be used in the App.


Results taking Bozho advice.

With Bozho changes Flex doesn't report the var type error any more, but once i run it. Adobe flash does report this.

TypeError: Error #1034: Type Coercion failed: cannot convert mx.utils::ObjectProxy@22cd311 to mx.collections.ArrayCollection. at PhotoGallery/getDataResultHandler()[C:\Users\Fábio Antunes\Documents\Flex Builder 3\Photo Gallery\src\ActionScripts\PhotoGallery.as:56] at PhotoGallery/__getData_result()[C:\Users\Fábio Antunes\Documents\Flex Builder 3\Photo Gallery\src\PhotoGallery.mxml:23] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.rpc.http.mxml::HTTPService/http://www.adobe.com/2006/flex/mx/internal%3A%3AdispatchRpcEvent%28%29%5BC:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\http\mxml\HTTPService.as:290] at mx.rpc::AbstractInvoker/http://www.adobe.com/2006/flex/mx/internal%3A%3AresultHandler%28%29%5BC:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AbstractInvoker.as:193] at mx.rpc::Responder/result()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\Responder.as:43] at mx.rpc::AsyncRequest/acknowledge()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\rpc\AsyncRequest.as:74] at DirectHTTPMessageResponder/completeHandler()[C:\autobuild\3.2.0\frameworks\projects\rpc\src\mx\messaging\channels\DirectHTTPChannel.as:403] at flash.events::EventDispatcher/dispatchEventFunction()

Well and the line 23 that Flash reports the error its:

PhotoData = ArrayCollection(event.result);

Line 23 is:

result="getDataResultHandler(event)"
A: 

You can simplify your script like this:

<mx:HTTPService id="getData" url="{XMLDataFileLocation}"/>
<mx:List dataProvider="{getData.lastResult.Photo}" labelField="name"/>

The lastResult of your getData will be the root of your XML. By retrieving lastResult.Photo you'll get an XMLList of the photos.

Ben Johnson
Yes i know that thanks, but the thing is that i use the list just to be sure that it was working, the list won't be used in the App.
Fábio Antunes
+1  A: 

If you can use an XMLListCollection in place of an ArrayCollection the process of converting the result object is more straightforward. Here is a good tutorial explaining how to go about this.

EDIT:

The key things to get from this tutorial are this:

  • you need to set the result format of the service to e4x.
  • you need to cast the result object as XML object, extract the repeating nodes as an XMLList, and construct an XMLListCollection from the list like so:

    private function httpService_result(evt:ResultEvent):void 
    {
         var xmlList:XMLList = XML(evt.result).path.to.repeating.element;
         xmlListColl = new XMLListCollection(xmlList);
    }
    
Ryan Lynch
I was trying a similar approach with the ArrayCollection. I will try with a XMLListCollection...
Fábio Antunes
They are interchangeable in most cases, with the `ArrayCollection` taking an `Array` as an argument, and the `XMLListCollection` taking an `XMLList`. I can't think of a general reason why you would want to mix the two together.
Ryan Lynch
Worked out. Thanks.
Fábio Antunes