天天看點

Sorting XML documents using an XMLListCollection

The following example shows how you can sort an XML document by converting it into an XMLListCollection and applying a sort. You can also easily reverse the current sort by calling the SortField object’s <code>reverse()</code> method and refreshing the XMLListCollection object

&lt;?xml version="1.0" encoding="utf-8"?&gt;

&lt;!-- http://blog.flexexamples.com/2007/12/04/sorting-xml-documents-using-an-xmllistcollection/ --&gt;

&lt;mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"

        layout="vertical"

        verticalAlign="middle"

        backgroundColor="white"&gt;

    &lt;mx:Script&gt;

        &lt;![CDATA[

            import mx.utils.StringUtil;

            private function list_labelFunc(item:Object):String {

                return StringUtil.substitute("{0} {1} ({2} {3})",

                        item.@label, // city

                        item.@name, // name

                        item.parent().parent().@abbrev, // league

                        item.parent().@label); // division

            }

            private function checkBox_click(evt:MouseEvent):void {

                sortField.reverse();

                xmlListColl.refresh();

        ]]&gt;

    &lt;/mx:Script&gt;

    &lt;mx:XML id="mlb" source="mlb.xml" /&gt;

    &lt;mx:XMLListCollection id="xmlListColl"

            source="{mlb.league.division.team}"&gt;

        &lt;mx:sort&gt;

            &lt;mx:Sort&gt;

                &lt;mx:fields&gt;

                    &lt;mx:SortField id="sortField"

                            name="@label"

                            caseInsensitive="true" /&gt;

                &lt;/mx:fields&gt;

            &lt;/mx:Sort&gt;

        &lt;/mx:sort&gt;

    &lt;/mx:XMLListCollection&gt;

    &lt;mx:ApplicationControlBar dock="true"&gt;

        &lt;mx:CheckBox id="checkBox"

                label="reverse()"

                labelPlacement="left"

                click="checkBox_click(event);" /&gt;

    &lt;/mx:ApplicationControlBar&gt;

    &lt;mx:List id="list"

            dataProvider="{xmlListColl}"

            labelFunction="list_labelFunc"

            width="300"

            rowCount="8" /&gt;

&lt;/mx:Application&gt;

    本文轉自 OldHawk  部落格園部落格,原文連結:http://www.cnblogs.com/taobataoma/archive/2008/01/13/1036987.html,如需轉載請自行聯系原作者