XML,XMLList,XMLListCollection之间的关系笔记

通常情况下,你可以从服务器上为tree控件取得XML数据 ,你也可以在<mx:Tree>Tag里直接定义格式良好的XML数据。

你可以使用<mx:XML>或者<mx:XMLList>Tag在mxml里定义XML数据。

你可以将XML object直接作为一个层级数据控件的dataProvider,however,if the object changes dynamically,你应该做如下处理:
1,将XML或者XMLList objects转换为XMLListCollection;
2,通过修改XMLListCollection数据来更新原始的XML数据;

XMLListCollection支持IList和ICollectionView两个接口,所以它能实现access,sort,filter等等操作:
get,set,add,remove
同时支持IViewCursor接口,于是可以实现Cursor功能:
一个Array,ArrayCollection,ICollectionView和IViewCursor之间的关系的例子如下:

var myAC:ArrayCollection=new ArrayCollection(myArray);
var myCursor:IViewCursor=myAC.CreateCursor();
while(!myCursor.afterLast){myCursor.moveNext();}

以下例子显示了两个Tree,一个使用XML作为数据源,一个使用XMLListCollection作为数据源

<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

    <mx:XML id="capitals">
        <root>
            <Capitals label="U.S. State Capitals">
                <capital label="AL" value="Montgomery"/>
                <capital label="AK" value="Juneau"/>
                <capital label="AR" value="Little Rock"/>
                <capital label="AZ" value="Phoenix"/>       
            </Capitals>
            <Capitals label="Canadian Province Capitals">
                <capital label="AB" value="Edmonton"/>
                <capital label="BC" value="Victoria"/>
                <capital label="MB" value="Winnipeg"/>
                <capital label="NB" value="Fredericton"/>
            </Capitals>
        </root>
    </mx:XML>

    <!-- Create an XMLListCollection representing the Tree nodes.
            多个capitals.Capitals组成XMLList,因为Capitals里还nest了其他 child elements. -->
    <mx:XMLListCollection id="capitalColl" source="{capitals.Capitals}"/>

    <!-- When you use an XML-based data provider with a tree you must specify
        the label field, even if it is "label".
        The XML object includes the root, so you must set showRoot="false".
        Remember that the Tree will not, by default, reflect dynamic changes
        to the XML object. -->
    <mx:Tree id="Tree1" dataProvider="{capitals}" labelField="@label"
showRoot="false" width="300"/>
    <!-- The XMLListCollection does not include the XML root. -->
    <mx:Tree id="Tree2" dataProvider="{capitalColl}" labelField="@label"
width="300"/>

</mx:Application>
从上面的例子可以看出,E4X标准的XML数据必须具有一个根节点,而为了阻止这个根节点在类似tree或者Menu-Based这样的层级数据显示控件显示根节点呢,我们必须设置showRoot属性为false;其次,当您使用XML,XMLList,XMLListCollection作为类似tree数据显示控件的数据源的时候,你 必须明确指定这些控件的labelField 属性,即使XMLattributes里有一个label,You must do this because you must use the @ sign to signify an attribute!XMLListCollection提供对数据源的dynamically updates,但是XMLList和XML不行。addItem,addItemAt,setItemAt,getItemIndex,removeItemAtXMLListCollection的CollectionEvent事件:public function collectionEventHandler(event:CollectionEvent):void {         switch(event.kind) {             case CollectionEventKind.ADD:                 addLog("Item "+ event.location + " added");                 break;             case CollectionEventKind.REMOVE:                 addLog("Item "+ event.location + " removed");                 break;             case CollectionEventKind.REPLACE:                 addLog("Item "+ event.location + " Replaced");                 break;             case CollectionEventKind.UPDATE:                 addLog("Item updated");                 break;         }}<mx:ArrayCollection id="ac"            collectionChange="collectionEventHandler(event)" />--------------------------------------------------------------------<?xml version="1.0">
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" height="400">
   
    <mx:Script>
    <![CDATA[
        import mx.collections.XMLListCollection;
        import mx.collections.ArrayCollection;
   
        // An XML object with categorized produce.
        [Bindable]
        public var myData:XML=
            <catalog>
                <category name="Meat">
                    <product name="Buffalo" cost="4" isOrganic="No"
                        isLowFat="Yes"/>
                    <product name="T Bone Steak" cost="6" isOrganic="No"
                        isLowFat="No"/>
                    <product name="Whole Chicken" cost="1.5" isOrganic="Yes"
                        isLowFat="No"/>
                </category>
                <category name="Vegetables">
                    <product name="Broccoli" cost="2.16" isOrganic="Yes"
                        isLowFat="Yes"/>
                    <product name="Vine Ripened Tomatoes" cost="1.69" isOrganic="No"
                        isLowFat="Yes"/>
                    <product name="Yellow Peppers" cost="1.25" isOrganic="Yes"
                        isLowFat="Yes"/>
                </category>
                <category name="Fruit">
                    <product name="Bananas" cost="0.95" isOrganic="Yes"
                        isLowFat="Yes"/>
                    <product name="Grapes" cost="1.34" isOrganic="No"
                        isLowFat="Yes" />
                    <product name="Strawberries" cost="2.5" isOrganic="Yes"
                        isLowFat="Yes"/>
                </category>
            </catalog>;
       
        // An XMLListCollection representing the data for the shopping List.
        [Bindable]
        public var listDP:XMLListCollection = new XMLListCollection(new XMLList());
   
        // Add the item selected in the Tree to the List XMLList data provider.
        private function doTreeSelect():void
        {
            if (prodTree.selectedItem)
                listDP.addItem(prodTree.selectedItem);
        }

        // Remove the selected in the List from the XMLList data provider.
        private function doListRemove():void
        {
            if (prodList.selectedItem)
                listDP.removeItemAt(prodList.selectedIndex);
        }

    ]]>
    </mx:Script>
   
    <mx:Tree id="prodTree" dataProvider="{myData}" width="200"
        showRoot="false" labelField="@name"/>
       
    <mx:HBox>
        <mx:Button id="treeSelect" label="Add to List"
            click="doTreeSelect()"/>
        <mx:Button id="listRemove" label="Remove from List"
            click="doListRemove()"/>
    </mx:HBox>
       
    <mx:List id="prodList" dataProvider="{listDP}" width="200"
        labelField="@name"/>

</mx:Application>


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/lentonnet/archive/2007/11/06/1869625.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值