Generally, you can obtain XML data for the tree control on the server. You can also define XML data in a good format in the <mx: Tree> tag.
You can use <mx: XML> or <mx: xmllist> tag to define XML data in mxml.
You can directly use an XML object as the dataprovider, however, and if the object changes dynamically of a hierarchical data control, you should do the following:
1. Convert XML or xmllist objects to xmllistcollection;
2. Update the original XML data by modifying the xmllistcollection data;
Xmllistcollection supports the ilist and icollectionview interfaces, so it can implement access, sort, filter, and other operations:
Get, set, add, remove
The iviewcursor interface is also supported, so you can implement the cursor function:
An example of the relationship between an array, arraycollection, icollectionview, and iviewcursor is as follows:
VaR myac: arraycollection = new arraycollection (myarray );
VaR mycursor: iviewcursor = myac. createcursor ();
While (! Mycursor. afterlast) {mycursor. movenext ();}
The following example shows two trees, one using XML as the data source and the other using xmllistcollection as the data source.
<? 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.
Multiple capitals. capitals form an xmllist, because other 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 names des 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>
The preceding example shows that the XML data of E4X must have a root node, to prevent the root node from displaying the root node in hierarchical data display controls such as tree or menu-based, we must set the showroot attribute to false. Secondly, when you use XML, when xmllist and xmllistcollection are data sources similar to tree data display controls, you must specify the labelfield attribute of these controls, even if xmlattributes contains a label, you must do this because you must use the @ sign to signify an attribute! Xmllistcollection provides dynamically updates for the data source, but xmllist and XML are not supported. Additem, additemat, setitemat, getitemindex, removeitematxmllistcollection's collectionevent event: 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)"/> events <? 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 = "maid" 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>
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/lentonnet/archive/2007/11/06/1869625.aspx