【问题标题】:Populating a datagrid in flex with content from an XML feed使用 XML 提要中的内容填充 flex 中的数据网格
【发布时间】:2011-04-10 21:22:10
【问题描述】:

我有一个数据网格:

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

我还有一个 REST 服务以以下格式返回 xml 文件:

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

现在,所有这些字段在返回时都会被填充,但为了更容易看到我已经删除了这些值。

我有一个 HTTPService 尝试使用以下结果函数填充它:

        private function searched(event:ResultEvent):void
        {
            var result:XML = XML(event.result);
            //summaryText.text = result.toXMLString();
            //Alert.show(result.children().children().children().toString() + "hey");
            resultsDataGrid.dataProvider = result.entry;
        }

我只需要将 Title 和 Updated 字段实际加载到 DataGrid 中。 这显然不起作用,所以我来寻求帮助,如果有人有经验并且可以告诉我如何正确安排它,我将不胜感激。 (我认为它与 result.entry 相关,它必须是其他类似 result.feed.entry 的东西,但我尝试了多种组合,但它们都没有奏效——除非我根本没有找到正确的组合。 )

【问题讨论】:

    标签: xml apache-flex datagrid mxml


    【解决方案1】:

    奇怪的是,命名空间是导致您的问题的原因。不包含前缀的命名空间会自动生成前缀。

    最简单的解决方案:不要使用命名空间。

    否则:你需要在这里做一些研究:http://livedocs.adobe.com/flex/3/langref/Namespace.html

    在以下示例代码中,尝试单击按钮,您会看到发生了什么:

    <?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>
    

    【讨论】:

    • 很抱歉花了这么长时间才回复您 - 但您完全正确。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-20
    • 2010-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多