Filtering an XMLListCollection using Regular Expressions

The following example shows how you can filter an XMLListCollection collection by using regular expressions in Flex 4.

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, see www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see 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.

For more information on the the API changes related to the removal of the “fx” prefix in Flex 4, see the “Dropping the Fx Prefix” page on opensource.adobe.com.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/04/01/filtering-an-xmllistcollection-using-regular-expressions/ -->
<s:Application name="XMLListCollection_filterFunction_RegExp_test"
        xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx"
        backgroundColor="white">
    <s:layout>
        <s:VerticalLayout horizontalAlign="center" verticalAlign="middle" />
    </s:layout>
 
    <fx:Script>
        <![CDATA[
            private function textInput_change():void {
                if (textInput.text.length == 0) {
                    xmlListColl.filterFunction = null;
                } else {
                    xmlListColl.filterFunction = xmlListColl_filterFunc;
                }
                xmlListColl.refresh();
            }
 
            private function xmlListColl_filterFunc(item:Object):Boolean {
                if (textInput.text.length == 0) return true;
                var f:String = "ig";
                var wasRegExp:RegExp = new RegExp(textInput.text, f);
                var nowRegExp:RegExp = new RegExp(textInput.text, f);
                var wasMatch:Boolean = wasRegExp.test(item.@was);
                var nowMatch:Boolean = nowRegExp.test(item.@now);
 
                return (wasMatch || nowMatch);
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <fx:XML id="nodesXML" source="nodes.xml" />
        <mx:XMLListCollection id="xmlListColl"
                source="{nodesXML.node}"
                filterFunction="xmlListColl_filterFunc" />
    </fx:Declarations>
 
    <s:controlBarContent>
        <mx:Form styleName="plain">
            <mx:FormItem label="Filter:">
                <s:TextInput id="textInput"
                        change="textInput_change();" />
            </mx:FormItem>
        </mx:Form>
    </s:controlBarContent>
 
    <mx:DataGrid id="dataGrid"
            dataProvider="{xmlListColl}"
            verticalScrollPolicy="on"
            width="90%"
            rowCount="10">
        <mx:columns>
            <mx:DataGridColumn dataField="@was" headerText="Was:" />
            <mx:DataGridColumn dataField="@now" headerText="Now:" />
        </mx:columns>
    </mx:DataGrid>
 
</s:Application>

And the nodes.xml data file is as follows:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/04/01/filtering-an-xmllistcollection-using-regular-expressions/ -->
<nodes>
    <node was="mx.components.ButtonBarButton" now="spark.components.ButtonBarButton" />
    <node was="mx.components.DataGroup" now="spark.components.DataGroup" />
    <node was="mx.components.DropDownList" now="spark.components.DropDownList" />
    <node was="mx.components.FxButton" now="spark.components.Button" />
    <node was="mx.components.FxButtonBar" now="spark.components.ButtonBar" />
    <!-- ... -->
</nodes>

[GoogleAdsWide]

View source is enabled in the following example.

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.

3 thoughts on “Filtering an XMLListCollection using Regular Expressions

Comments are closed.