Blog Stats
  • Posts - 8
  • Articles - 0
  • Comments - 104
  • Trackbacks - 0

 

Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

I am learning Flex 2 for a project I'm working on with a friend.  We decided, for simplicity's sake, to have our web service connection to the back-end be relatively simple to begin with:  Just use HTTP POST.  But we ran into a snag, which, in doing a search on Google, I discovered that many others have run into.

When you use a web service to pull a list of XML nodes into Adobe Flex, what happens is that Flex generates a "result" object that contains the xml you're trying to read in.  As the Flex developer, you're expected to declare an object of type ArrayCollection, set it equal to the ArrayCollection in the Result collection, and you're good to go.  Frustratingly, that's where a lot of the documentation I found on this ends, and it's not the whole picture.  The problem is with that ArrayCollection in the Result object that Flex creates for you:  The problem is it does NOT always do it!  It only does it in cases where it "has to," that is, for xml containing node lists with a count of 2 or more.

More specifically:

  • For node lists of two or more nodes, Flex recognizes that it "needs" an ArrayCollection to contain them, and creates the ArrayCollection in the Result object to contain them.
  • For node lists of exactly one node, Flex recognizes that it "needs" to only create a single-object container, and chooses to do this by creating an object of type ObjectProxy in the Result object to contain it.  For consistency in your application, I would recommend that you use Flex' toArray() function to convert the ObjectProxy to ArrayCollection. 
  • For node lists of exactly zero nodes, Flex decides, rightly so, to set its internal representation of the node collection to NULL.  In that case, for consistency, I would recommend that you simply create an empty ArrayCollection.

Sample code is below.  I've included the web service "mx:HTTPService" definition and its call to the parsing function.

 

    <mx:Script>
    <![CDATA[
        import mx.collections.ArrayCollection;
        import mx.utils.ObjectProxy;
        import mx.rpc.events.ResultEvent;
        import mx.controls.Alert;
        import mx.utils.ArrayUtil;
        [Bindable]
        private var reminderList:ArrayCollection;
        private function ParseReminderList(event:ResultEvent):void
        {               
            if (event.result.Reminders == null)
            {
                reminderList=new ArrayCollection()
            }
            else if (event.result.Reminders.Reminder is ArrayCollection)
            {
              reminderList=event.result.Reminders.Reminder;
            }
            else if (event.result.Reminders.Reminder is ObjectProxy)
            {
               reminderList = new ArrayCollection(ArrayUtil.toArray(event.result.Reminders.Reminder));
            }
        }
    ]]>
        </mx:Script>
<mx:HTTPService id="ReminderListService" url="http://localhost/reminders.asp" result="ParseReminderList(event)"/>


Feedback

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar

ps. -- Also as part of my researches in Google on this, this arrangement is almost univesally considered a bug.  I don't agree with that.  It is, at most, a feature, and I'd even argue with that, too.  Once you understand what Flex is actually doing with this under the covers and why, it makes sense.  Yes, it's less convenient for us.  Yes, Adobe should strongly consider including a helper function in the next version of ActionScript that will do this for us.  And, God yes, they need to document this better.  But in terms of what Flex/ActionScript is actually doing, I wouldn't mess with it.  It's clean and logical.  And that's a breath of fresh air, to me.

2/26/2008 9:35 AM | ebuatois

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar Woaa..thanks, i dont know if this is bug or not, but clearly FLEX doesn't documented this well...
Thanks for your help... 10/14/2008 1:06 AM | Martinus J Wahyudi

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar Thank you so much. Been pounding my head for days. Good Job! I will reference this on my blog. 10/14/2008 4:09 PM | Arnold Aprieto

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar Hi,
I checked so many sites to check the type of the Object I received from HttpService in my case. Here I found the solution and it works fine and solved all my troubles.
Thanks a lot.



12/30/2008 12:17 AM | Srinivas Maddipatla

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar 1. Please visit www.busycode.com or www.flexdeveloper.com.cn
2. Low cost high quality.
3. Best Adobe Flex outsourcing service provider.
4. More than 60 full time in-house Flex developers.
5. Our company Busycode Inc. was registered in San Francisco, Beijing and Nanning.
6. Our skill set is "Adobe Flex/AIR/Flash + .NET/Java/PHP + SQL Database".
7. Our keywords are Flex developer, Flex coder, Flex programmer, Flex expert, Flex engineer, Flex specialist and Flex outsourcing service provider. 2/22/2009 9:46 PM | du

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar The syntax is easy enough to understand, just replace my links with the important pages of your site. My site map is here, feel free to copy and paste and edit to your liking. Once you are done, place the site map.xml in your home directory and submit to Google. The site map program will tell you where your page is ranked, what search results show your page, and if your site has any errors, like broken links.
3/13/2010 4:35 AM | casino de blackjack en ligne

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar This is my first time i visit here. I found so many interesting stuff in your blog. I guess I am not the only one having all the enjoyment here! keep up the good work. 5/21/2010 5:45 AM | convert miles to km

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar



This writer is an amazing example of a natural talent
bates boots
6/2/2010 4:26 AM | ADAM

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar well, ok, i really need to know more about it, don't you think so? 10/16/2010 1:46 PM | ephedrine

# Offshore Outsource

Gravatar This information that you have shared with us is excellent, I'm glad i came to this site, Once again thank you! http://www.telegenisys.com/ 11/11/2010 1:43 PM | Outsourcing Companies

# Offshore Outsource

Gravatar So far, I managed to go though only some of posts you discuss here, but I find them very interesting and informative. Just want say thank you for the information you have shared. Regards. http://www.telegenisys.com/ 11/11/2010 1:44 PM | Outsourcing Companies

# canada goose on sale

Gravatar Christmas is approaching, more and more cold weather, men's down jacket is popular the market.cheap canada goose Personalized design, conspicuous without publicity color, warm down material, let you all winter leisure fashion. When Christmas comes, you can not only enjoy the food, beauty, can also hold the lady. Sports leisure down jacket. Bright skin fresh blue make you all know, in the dark winter was particularly conspicuous.canada goose montebello parka When Christmas comes, down jacket inside the ride a T-shirt can let you image auspicious went out on a date. Super personalized down jacket. Patent leather and feather clothing thermal binding, let your whole image of warm and fashion.canada goose on sale Seems to be lines lines just modified the whole coat, with light skin and grey stronger bring out your youthful vitality. A bubble shape very sports. Handsome black down jacket.Buy canada goose online Low profile black because with bright skin design, let the whole down instantly brighten up.Canada Goose Kensington Parka A coupled with hooded processing, will be your age frames in the vibrant youth period, with wool hat, together with the casual jeans, can make you very handsome very cool.
10/28/2011 5:15 AM | canada goose on sale

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar When everything else physical and mental seems to diminish, the appreciation of beauty is on the increase.

bigcommerce
10/29/2011 6:38 AM | SHONA

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar Keep writing the blog article regular. And on a daily basis I'd like to read merely your blog upon internet.
dating sites 3/31/2012 11:49 AM | liza

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar Knowledgeable blogs will be always welcome. This blog holds great informative stuff. You will need to read that one and appreciate. Thanks.
dating sites 3/31/2012 11:50 AM | liza

# re: Web Services and ArrayCollections in Adobe FLEX 2: How to Successfully Read Any Number of XML Nodes

Gravatar This blog helped me to solve my problem to deal with the arrays inside my XML result from my webservice. I was able to get simple informations but not the information inside the array structures.

Thanks a lot. 10/4/2012 10:12 AM | Antonio (Brazil)

Post a comment





 

Please add 3 and 4 and type the answer here:

 

 

Copyright © Edward Buatois