Using an XML data provider with the Spark List control in Flex 4

Using an XML data provider with the Spark List control in Flex 4

by PETER DEHAAN on NOVEMBER 4, 2009

in LIST (SPARK), XML, XMLLIST, XMLLISTCOLLECTION, BETA2

The following example shows how you can use an XML document as a data provider for a Spark List control in Flex 4 by using an XMLListCollection.

Full code after the jump.

 

The following example(s) require Flash Player 10 and the Adobe Flex 4 SDK. To download the Adobe Flash Builder 4 trial, seehttp://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, seehttp://opensource.adobe.com/wiki/display/flexsdk/Download+Flex+4.
For more information on getting started with Flex 4 and Flash Builder 4, see the official Adobe Flex Team blog.

<?xml version="1.0"?> <!-- http://blog.flexexamples.com/2009/11/04/using-an-xml-data-provider-with-the-spark-list-control-in-flex-4/ --> <s:Application name="Spark_List_dataProvider_XML_test" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo">   <s:List id="lst" labelField="@label" horizontalCenter="0" verticalCenter="0"> <s:dataProvider> <s:XMLListCollection> <fx:XMLList xmlns=""> <node label="One" /> <node label="Two" /> <node label="Three" /> <node label="Four" /> <node label="Five" /> <node label="Six" /> <node label="Seven" /> <node label="Eight" /> <node label="Nine" /> </fx:XMLList> </s:XMLListCollection> </s:dataProvider> </s:List>   </s:Application>
 
  

Or, if you wanted to embed the XML into your application, you could use the <fx:XML/> tag and bind to an XMLListCollection, as seen in the following example:

<?xml version="1.0"?> <!-- http://blog.flexexamples.com/2009/11/04/using-an-xml-data-provider-with-the-spark-list-control-in-flex-4/ --> <s:Application name="Spark_List_dataProvider_XML_test" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo">   <fx:Declarations> <fx:XML id="nodes" source="nodesAndStuff.xml" /> </fx:Declarations>   <s:List id="lst" labelField="@label" horizontalCenter="0" verticalCenter="0"> <s:dataProvider> <s:XMLListCollection source="{nodes.children()}" /> </s:dataProvider> </s:List>   </s:Application>
 
  

And the external .XML file, nodesAndStuff.xml, is as follows:

<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2009/11/04/using-an-xml-data-provider-with-the-spark-list-control-in-flex-4/ --> <root> <node label="One" /> <node label="Two" /> <node label="Three" /> <node label="Four" /> <node label="Five" /> <node label="Six" /> <node label="Seven" /> <node label="Eight" /> <node label="Nine" /> </root>
 
  

Or, if you didn’t want to use data binding, Corey, you could set the data provider using ActionScript, as seen in the following example:

<?xml version="1.0"?> <!-- http://blog.flexexamples.com/2009/11/04/using-an-xml-data-provider-with-the-spark-list-control-in-flex-4/ --> <s:Application name="Spark_List_dataProvider_XML_test" xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/halo" initialize="init();">   <fx:Script> <![CDATA[ private function init():void { xmlListColl.source = nodes.children(); } ]]> </fx:Script>   <fx:Declarations> <fx:XML id="nodes" source="nodesAndStuff.xml" /> </fx:Declarations>   <s:List id="lst" labelField="@label" horizontalCenter="0" verticalCenter="0"> <s:dataProvider> <s:XMLListCollection id="xmlListColl" /> </s:dataProvider> </s:List>   </s:Application>
 
  

This entry is based on a beta version of the Flex 4 SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex 4 SDK.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值