Merging XMLListCollections in Flex

The following example shows how you can combine two XMLListCollection objects in Flex by using the addAll() method.

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.

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/10/28/merging-xmllistcollections-in-flex/ -->
<s:Application name="Spark_XMLListCollection_addAll_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:controlBarContent>
        <s:Button id="btn"
                label="Merge XMLListCollections"
                click="button1_clickHandler(event);" />
    </s:controlBarContent>
 
    <fx:Script>
        <![CDATA[
            protected function button1_clickHandler(evt:MouseEvent):void {
                xmlListColl1.addAll(xmlListColl2);
            }
        ]]>
    </fx:Script>
 
    <fx:Declarations>
        <s:XMLListCollection id="xmlListColl1">
            <fx:XMLList xmlns="">
                <item label="1. One" />
                <item label="1. Two" />
                <item label="1. Three" />
                <item label="1. Four" />
                <item label="1. Five" />
            </fx:XMLList>
        </s:XMLListCollection>
        <s:XMLListCollection id="xmlListColl2">
            <fx:XMLList xmlns="">
                <item label="2. Six" />
                <item label="2. Seven" />
                <item label="2. Eight" />
                <item label="2. Nine" />
            </fx:XMLList>
        </s:XMLListCollection>
    </fx:Declarations>
 
    <s:List id="lst"
            dataProvider="{xmlListColl1}"
            labelField="@label"
            alternatingItemColors="[#DFDFDF,#EEEEEE]"
            horizontalCenter="0" verticalCenter="0">
        <s:layout>
            <s:VerticalLayout gap="0"
                    horizontalAlign="contentJustify"
                    requestedRowCount="10" />
        </s:layout>
    </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.

Or, if you are using Flex 3, you could use the following code:

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/10/28/merging-xmllistcollections-in-flex/ -->
<mx:Application name="XMLListCollection_addAll_test"
        xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">
    <mx:ApplicationControlBar dock="true">
        <mx:Button id="btn"
                label="Merge XMLListCollections"
                click="button1_clickHandler(event);" />
    </mx:ApplicationControlBar>
 
    <mx:Script>
        <![CDATA[
            protected function button1_clickHandler(evt:MouseEvent):void {
                xmlListColl1.addAll(xmlListColl2);
            }
        ]]>
    </mx:Script>
 
    <mx:XMLListCollection id="xmlListColl1">
        <mx:XMLList xmlns="">
            <item label="1. One" />
            <item label="1. Two" />
            <item label="1. Three" />
            <item label="1. Four" />
            <item label="1. Five" />
        </mx:XMLList>
    </mx:XMLListCollection>
    <mx:XMLListCollection id="xmlListColl2">
        <mx:XMLList xmlns="">
            <item label="2. Six" />
            <item label="2. Seven" />
            <item label="2. Eight" />
            <item label="2. Nine" />
        </mx:XMLList>
    </mx:XMLListCollection>
 
    <mx:List id="lst"
             dataProvider="{xmlListColl1}"
             labelField="@label"
             rowCount="10"
             alternatingItemColors="[#DFDFDF,#EEEEEE]"
             width="100" />
 
</mx:Application>

3 thoughts on “Merging XMLListCollections in Flex

  1. I think the “addAll” function doesn’t exist in flex 3, so your example is not compiling for me :(

Comments are closed.