views:

658

answers:

3

Hi!

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

+2  A: 

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

Sean Clark Hess
Was trying this recommendation: so added: import mx.collections.XMLListCollection;[Bindable]public var claims:XMLListCollection;claims = new XMLListCollection(event.result.my_claims.claim);To the code, but it throws an exceptionTypeError: Error #1034: Type Coercion failed: cannot convert mx.utils::ObjectProxy@35ca2701 to XMLList. at goclient::ListOfPlayers/resultHandler()[/Users/oleg/Documents/FB/usersList/src/goclient/ListOfPlayers.as:59] at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent()Could you please help?
Oleg Tarasenko
Oh, my bad... You need to load the data as e4x instead of an object. Are you using HTTPService? <mx:HTTPservice resultFormat="e4x"/>.
Sean Clark Hess
Thanks for the answer.Now I get this error:[RPC Fault faultString="Error #1088: The markup in the document following the root element must be well-formed." faultCode="Client.CouldNotDecode" faultDetail="null"]Here is how I created Service:usersListService = new HTTPService();usersListService.url = "http://127.0.0.1:8000/go/active/";usersListService.resultFormat = "e4x";Maybe something with my XML data I send from the server?
Oleg Tarasenko
<? xml version="1.0" ?><current> <username> test1 </username> <img>http://127.0.0.1:8000/site_media/images/js_jpg_150x150_q85.jpg</img> <rank>2000</rank></current><my_claims> <claim> <opponent>Oleg</opponent> <rank>2000</rank> </claim></my_claims><players> <player> <name>test</name> <status>F</status> <claimed>False</claimed> </player> </players>
Oleg Tarasenko
hm... Is that possible to format code in comments?
Oleg Tarasenko
Every XML Document needs to have a root tag surrounding everything. Your document opens with <current> and ends with </players>. They need to be the same tag
Sean Clark Hess
+1  A: 

try setting your arraycollection like this:

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

If you result only has 1 item you get this error

Shua
I just tried this solution. Everything seems to work, and claims variable is populated with list from XML service.But seems that I can't use it as dataprovider for datagrid, in case I have only one item in the list...?
Oleg Tarasenko
Please ignore last comment. It works fine here!Thanks!
Oleg Tarasenko
+1  A: 

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.

James Fassett