1

my script.php returns this XML

<all>
  <item>
     <field1>value1</field1>
     <field2>value2</field2>
  </item>

  <item>
     <field1>value1</field1>
     <field2>value2</field2>
  </item>
</all>

The HTTPService uses the default resultFormat="object" but I don't declare it since it's the default.

Then I bind it to a List

dataProvider="{getDataHTTP.lastResult.all.item}"

I get no problems when the number of item returned is more than 1. But when it's only 1 item I get an error cannot convert XMLList to mx.collections.IList.

I tried different solutions including trying to cast it as XMLListCollection but it still gives an error for single items. Does anyone know of a way to possibly solve this?

tag
  • 3,315
  • 2
  • 16
  • 6

2 Answers2

2

Make resultFormat="xml" and set dataProvider="{getDataHTTP.lastResult.item}"

Amarghosh
  • 58,710
  • 11
  • 92
  • 121
  • Unfortunately did not work. When I do that I get no output at all, even in the case of multiple `item`. Pulling my hair on it. – tag May 10 '10 at 05:12
  • @tag try `resultFormat="e4x"` – Amarghosh May 10 '10 at 05:18
  • @tag: I think, the problem is, that XML is not bindable. you are likely to even get a warning at runtime, that a binding couldn't be created. As Amargosh said, you need to specify the format, so that flex can wrap it into some bindable proxies. – back2dos May 10 '10 at 07:21
  • If the `HTTPService` instance is bindable (which it'll be if it is declared in mxml - or if it has the `[Bindable]` meta tag), then it shouldn't be a problem as the `lastResult` is anyway bindable. – Amarghosh May 10 '10 at 07:27
  • I doubt it's an issue of Bindable, because it works fine when the number of `item` returned is more than 1, so it's working. I get the error only when it's a single `item` returned. – tag May 10 '10 at 13:04
1
import mx.rpc.xml.SimpleXMLDecoder;
import mx.rpc.xml.SimpleXMLEncoder;

[Bindable]public var xmlDataObj:Object = new Object(); 

private function yourResultEvent(evt:ResultEvent):void{
var resultXml:XMLDocument = new XMLDocument(evt.result as String);
var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
xmlDataObj= decoder.decodeXML(resultXml).all.item;
}

This way you don't need to worry about changing your resultFormat to XML or e4x.

Thalaivar
  • 23,282
  • 5
  • 60
  • 71