?
Solved

Flex issue with single-element vs multi-element

Posted on 2009-04-21
4
Medium Priority
?
436 Views
Last Modified: 2012-06-21
For those of you who have been using Flex since 1.5, do you remember how Flex always had a problem returning single-element arrays as a non-array object, and how we had to apply mx.utils.ArrayUtils to force it to become a one-element array?

It seems like Flex 3.0 still has the same problem. If the XML I am getting from the server looks like this:

<root>
    <node ..../>
    <node ..../>
<root>

that is, a root tag with more than one child node, event.result.root.node will be of type ArrayCollection. But if the XML is like this:

<root>
    <node ..../>
</root>

where there is only one child tag, event.result.root.node becomes of type mx.utils.ObjectProxy. This is causing my application to bomb because I'm automatically storing the results in an ArrayCollection.

Has anyone had this problem before? Any suggestions on how to handle this would be greatly appreciated.

Thanks.
0
Comment
Question by:elepil
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
lexxwern earned 1500 total points
ID: 24201457
>> that is, a root tag with more than one child node, event.result.root.node will be
>> of type ArrayCollection. But if the XML is like this:

I believe in this case the event.result.root.node will now (Flex 2 & 3) be of type XMLList even if there are multiple or single node objects in the XML. So I think this should solve your problem if you can convert the XMLList to an ArrayCollection.

Or you can consider using an XMLListCollection which is an implementation of a Collection onto an XMLList.
0
 

Author Closing Comment

by:elepil
ID: 31573096
You are right, that storing the xml data coming from the server in an XMLListCollection would avoid this problem. While waiting for a response on this issue, I changed my HTTPService's resultFormat to 'e4x', received the data in XML format, then *manually* parsed it to construct an ArrayCollection. I rated 'partial' to your solution because you didn't show how to convert an XMLListCollection object to an ArrayCollection. Because I did it differently from the way you're recommending (and my way was tedious, too), it would've been nice to see if there was an easy and quick way to convert an XMLListCollection to an ArrayCollection.

Nevertheless, I gave you full credit anyway.
0
 
LVL 12

Expert Comment

by:lexxwern
ID: 24201554
>> it would've been nice to see if there was an easy and quick way to convert
>> an XMLListCollection to an ArrayCollection

Yeah probably you could just explicitly type cast it.. try something like this:

   var newArrayCollection:ArrayCollection = theXmlListCollection as ArrayCollection;
   // or
   var newArrayCollection:ArrayCollection = ArrayCollection(theXmlListCollection);

0
 

Author Comment

by:elepil
ID: 24201639
I didn't expect XMLListCollection and ArrayCollection to be interchangeably cast-able; I am glad it is that easy. Thanks for following up.
0

Featured Post

Cyber Threats to Small Businesses (Part 1)

This past May, Webroot surveyed more than 600 IT decision-makers at medium-sized companies to see how these small businesses perceived new threats facing their organizations.  Read what Webroot CISO, Gary Hayslip, has to say about the survey in part 1 of this 2-part blog series.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

First things first - Preparation We need all the part for this install and it's much nicer to have them all on hand when you need them so here's what's required. Download Eclipse 3.5 32 bit (I like the Classic flavour) from here. (http://www.e…
Last month Marc Laliberte, WatchGuard’s Senior Threat Analyst, contributed reviewed the three major email authentication anti-phishing technology standards: SPF, DKIM, and DMARC. Learn more in part 2 of the series originally posted in Cyber Defense …
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question