0
votes

I have following problem:

My webservice application returns xml data in following order:

<my_claims>
  <claim>
    <opponent>Oleg</opponent>
    <rank>2000</rank>
  </claim>
</my_claims>

Where number of claim nodes, can be 0,1, and so on.

How I correctly handle, the data received from the service. Currently, when I am trying to store claims data as an array collection, e.g.

this.Claims = event.result.my_claims.claim;

public function set Claims(array:ArrayCollection):void
{
    this.claims = array;
}

I am getting the error:

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

As far as I understand Flex handles this as an XmlObject, but after I have several items in the list from service, everything works fine:

(Example with several claims) Oleg 2000 Test 2000

3

3 Answers

2
votes

Use an xmllist collection instead. this.Claims = new XMLListCollection(event.result.my_claims.claim); You'll need to update your Claims function to match

1
votes

try setting your arraycollection like this:

if( event.result.my_claims.claim is ArrayCollection ){
  this.claims = event.result.my_claims.claim as ArrayCollection;

}
else{
  this.claims = new ArrayCollection( [event.result.my_claims.claim] );
}

If you result only has 1 item you get this error

1
votes

I've come across this myself before. Adobe's documentation on Traversing XML structures isn't exactly clear on the point.

Their documentation states that if there is more than one element with a particular name you have to use array index notation to access it:

var testXML:XML = <base>
    <book>foo</book>
    <book>bar</book>
</base>

var firstBook:XML = testXML.book[0];

Then it goes on to say that if there is only one element with a particular name then you can omit the array index notation:

var testXML:XML = <base>
    <book>foo</book>
</base>

var firstBook:XML = testXML.book;

This means that when you try and force coercion to an Array type it doesn't work since it sees the single element as an XMLNode and not an XMLList.

If you are lucky you can just check the number of children on your <my_claims> node and decide if you want to warp the single element in an ArrayCollection or if you can use the automatic coercion to work for multiple elements.