Search code examples
apache-flexactionscript-3httpservice

Flex: Processing xml, before using it in datagrid


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


Solution

  • 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