views:

29

answers:

2

I have this XML (Flash/AS3):

 <channel>
 <title>...</title>
 <description>...</description>
 <item><summary>...</summary><detail>...</detail></item>
 <item><summary>...</summary><detail>...</detail></item>
 ...
 </channel>

I want to create a DataProvider containing the elements (for use in a datagrid).

I thought this would work:

var items:XML = new XML(evt.target.data); //url loader event listener 'complete'
trace(items..item is XMLList); // true
myDP = new DataProvider(items..item);

But I get this error:

 TypeError: Error: Type Coercion failed: cannot convert 
   <item><summary>...</summary><detail>...</detail></item>
   <item><summary>...</summary><detail>...</detail></item>
     ...
 to Array or DataProvider.
    at fl.data::DataProvider/getDataFromObject()
    at fl.data::DataProvider()

What am I doing wrong?

A: 

You don't need to create an instance of DataProvider. You should be able to directly assign the dataProvider property with the XML instance. The dataProvider setter will automatically determine the object type and handle it appropriately.

John Drummond
So I tried this: myData.dataProvider = items..item; (myData is a datagrid) and I receive: TypeError: Error #1034: Type Coercion failed: cannot convert XMLList@af7f2b1 to fl.data.DataProvider.
Scott
Please read: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/controls/listClasses/ListBase.html#dataProvider are you using mx.controls.DataGrid?
John Drummond
A: 

You have to give the DataProvider an XML, not an XMLList:

var items:XML = <channel>
   <title>Title</title>
   <description>Description</description>
   <item><summary>Summary 1</summary><detail>Detail 1</detail></item>
   <item><summary>Summary 2</summary><detail>Detail 2</detail></item>
</channel>;

var xml:XML = <dummy/>;
xml.appendChild(items..item);
list.dataProvider = new DataProvider(xml);
list.labelFunction = function(item:Object) {
   return item.summary;
}
Claus Wahlers
Thanks, that worked.
Scott