Question

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?

Was it helpful?

Solution

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;
}

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top