Populating a datagrid in flex with content from an XML feed
I have a datagrid:
<mx:DataGrid id="resultsDataGrid"
height="328" width="604" paddingRight="0" editable="false" y="43" horizontalCenter="0">
<mx:columns>
<mx:DataGridColumn headerText="Title" dataField="title"/>
<mx:DataGridColumn headerText="Updated" dataField="updated"/>
</mx:columns>
</mx:DataGrid>
I also have a REST service returning an xml file in the following format:
<feed xmlns="http://www.w3.org/2005/Atom">
<title></title>
<link href=""/>
<link rel="" href=""/>
<updated></updated>
<author>
<name></name>
</author>
<id></id>
<entry>
<title></title>
<link href=""/>
<id></id>
<updated></updated>
<published></published>
<summary></summary>
<author>
<name></name>
</author>
<category term="" label=""/>
<category term="" scheme=""/>
</entry>
</feed>
Now, all of those fields are populated when its returned but to keep it simpler to see I've removed the values.
I have an HTTPService trying to populate it, with the following result function:
private function searched(event:ResultEvent):void
开发者_如何学JAVA {
var result:XML = XML(event.result);
//summaryText.text = result.toXMLString();
//Alert.show(result.children().children().children().toString() + "hey");
resultsDataGrid.dataProvider = result.entry;
}
I only need the Title and Updated fields to actually be loaded into the DataGrid. This clearly doesn't work, so I've come asking for help, if anyone has experience and can tell me how to arrange it correctly, I'd appreciate it. (I think it's related to result.entry, that it must be something else like result.feed.entry, but I've tried a number of combinations and they haven't worked - unless I simply haven't hit the right one.)
The namespace, oddly enough, is what is causing your problem. Prefixes are automatically generated for namespaces that don't include them.
Easiest solution: don't use the namespace.
Otherwise: you will need to do some research here: http://livedocs.adobe.com/flex/3/langref/Namespace.html
In the following example code try clicking the buttons, you'll see what's happening:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:XML id="atomNamespace">
<feed xmlns="http://www.w3.org/2005/Atom">
<title></title>
<link href="" />
<link rel="" href="" />
<updated></updated>
<author>
<name></name>
</author>
<id></id>
<entry>
<title>Title text</title>
<link href="" />
<id></id>
<updated>2010-09-10</updated>
<published></published>
<summary></summary>
<author>
<name></name>
</author>
<category term="" label="" />
<category term="" scheme="" />
</entry>
</feed>
</mx:XML>
<mx:XML id="noNamespace">
<feed>
<title></title>
<link href="" />
<link rel="" href="" />
<updated></updated>
<author>
<name></name>
</author>
<id></id>
<entry>
<title>Title text</title>
<link href="" />
<id></id>
<updated>2010-09-10</updated>
<published></published>
<summary></summary>
<author>
<name></name>
</author>
<category term="" label="" />
<category term="" scheme="" />
</entry>
</feed>
</mx:XML>
<mx:DataGrid id="resultList">
<mx:columns>
<mx:DataGridColumn headerText="Title" dataField="title" />
<mx:DataGridColumn headerText="Updated" dataField="updated" />
</mx:columns>
</mx:DataGrid>
<mx:Button label="Atom Namespace" click="resultList.dataProvider = atomNamespace..entry"/>
<mx:Button label="No Namespace" click="resultList.dataProvider = noNamespace..entry"/>
</mx:Application>
精彩评论