Using an XML data provider with the Spark List control in Flex 4
The following example shows how you can use an XML document as a data provider for a Spark List control in Flex 4 by using an XMLListCollection.
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 http://www.adobe.com/products/flex/. To download the latest nightly build of the Flex 4 SDK, see http://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"?> <!-- http://blog.flexexamples.com/2009/11/04/using-an-xml-data-provider-with-the-spark-list-control-in-flex-4/ --> <s:Application name="Spark_List_dataProvider_XML_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:List id="lst" labelField="@label" horizontalCenter="0" verticalCenter="0"> <s:dataProvider> <s:XMLListCollection> <fx:XMLList xmlns=""> <node label="One" /> <node label="Two" /> <node label="Three" /> <node label="Four" /> <node label="Five" /> <node label="Six" /> <node label="Seven" /> <node label="Eight" /> <node label="Nine" /> </fx:XMLList> </s:XMLListCollection> </s:dataProvider> </s:List> </s:Application>
Or, if you wanted to embed the XML into your application, you could use the <fx:XML/> tag and bind to an XMLListCollection, as seen in the following example:
<?xml version="1.0"?> <!-- http://blog.flexexamples.com/2009/11/04/using-an-xml-data-provider-with-the-spark-list-control-in-flex-4/ --> <s:Application name="Spark_List_dataProvider_XML_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"> <fx:Declarations> <fx:XML id="nodes" source="nodesAndStuff.xml" /> </fx:Declarations> <s:List id="lst" labelField="@label" horizontalCenter="0" verticalCenter="0"> <s:dataProvider> <s:XMLListCollection source="{nodes.children()}" /> </s:dataProvider> </s:List> </s:Application>
And the external .XML file, nodesAndStuff.xml, is as follows:
<?xml version="1.0" encoding="utf-8"?> <!-- http://blog.flexexamples.com/2009/11/04/using-an-xml-data-provider-with-the-spark-list-control-in-flex-4/ --> <root> <node label="One" /> <node label="Two" /> <node label="Three" /> <node label="Four" /> <node label="Five" /> <node label="Six" /> <node label="Seven" /> <node label="Eight" /> <node label="Nine" /> </root>
Or, if you didn’t want to use data binding, Corey, you could set the data provider using ActionScript, as seen in the following example:
<?xml version="1.0"?> <!-- http://blog.flexexamples.com/2009/11/04/using-an-xml-data-provider-with-the-spark-list-control-in-flex-4/ --> <s:Application name="Spark_List_dataProvider_XML_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" initialize="init();"> <fx:Script> <![CDATA[ private function init():void { xmlListColl.source = nodes.children(); } ]]> </fx:Script> <fx:Declarations> <fx:XML id="nodes" source="nodesAndStuff.xml" /> </fx:Declarations> <s:List id="lst" labelField="@label" horizontalCenter="0" verticalCenter="0"> <s:dataProvider> <s:XMLListCollection id="xmlListColl" /> </s:dataProvider> </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.
Peter deHaan
Peter deHaan currently works for Adobe on the Flex SDK QA team. While not working on Flex, Flash, and ColdFusion applications, Peter enjoys making up bios and writing in 3rd person. Peter's rarely updated blog can be found at blogs.adobe.com/pdehaan/, actionscriptexamples.com, airexamples.com, and coldfusionexamples.com.
-
Add Widgets (Content Sidebar)
This is your Content Sidebar. Edit this content that appears here in the widgets panel by adding or removing widgets in the Content Sidebar area.
20 Responses to Using an XML data provider with the Spark List control in Flex 4
Leave a Reply Cancel reply
-
Categories
- Accordion
- AccordionHeader
- ActionScript
- AddChild
- AdvancedDataGrid
- Alert
- alpha
- Animate
- AnimateProperties
- Application
- Application (Spark)
- ArrayCollection
- BarChart
- baseColor
- beta
- beta1
- beta2
- Bitmap
- Bitmap/BitmapData
- BitmapData
- BitmapFill
- BitmapFill (Spark)
- BitmapGraphic
- BitmapImage
- BitmapImage (Spark)
- BitmapImageResizeMode
- Border (Spark)
- BorderContainer (Spark)
- Box
- BuildInfo
- Button
- Button (Spark)
- ButtonBar
- ButtonBar (Spark)
- ByteArray
- Camera
- Charting
- CheckBox
- CheckBox (Spark)
- ClassFactory
- CollectionEvent
- Color
- ColorPicker
- ColorUtil
- ComboBox
- ComboBoxArrowSkin
- Compiler
- Component
- Component (Spark)
- Configuration
- Container
- ContextMenu
- ContextMenuEvent
- ContextMenuItem
- CSSCondition
- CSSSelector
- CSSStyleDeclaration
- CurrencyFormatter
- CursorManager
- Data Binding
- DataGrid
- DataGrid (Spark)
- DataGridColumn
- Date
- DateBase
- DateChooser
- DateField
- DateFormatter
- Debugging
- DefaultComplexItemRenderer
- DefaultTileListEffect
- DropDownList
- DropDownList (Spark)
- DropDownListButtonSkin
- DropDownListSkin
- DropShadowFilter
- E4X
- Effects
- Ellipse
- EmailValidator
- Embed
- Event
- Fade
- FileFilter
- FileReference
- fill
- Filters
- Flash
- Flash Integration
- FlashVars
- Flex 3 SDK
- Flex Builder
- Flex Builder 3
- Flex SDK
- Flex4
- FLVPlayback
- FocusManager
- FontLookup
- Fonts
- Form
- Form (Spark)
- FormHeading (Spark)
- FormItem
- FormItem (Spark)
- Forms
- FTETextField (Spark)
- FullScreen
- FullScreenEvent
- FxAnimateColor
- FxButtonBar
- FxCheckBox
- FXG
- FxHScrollBar
- FxHSlider
- FxList
- FxNumericStepper
- FxRadioButton
- FxRotate3D
- FxScroller
- FxTextArea
- FxTextInput
- FxToggleButton
- FxVScrollBar
- FxVSlider
- getStyleDeclaration()
- GradientEntry
- Graphic (Spark)
- HBox
- HDividedBox
- HGroup (Spark)
- HorizontalLayout
- HorizontalList
- HSBColor (Spark)
- HScrollBar (Spark)
- HSlider
- HSlider (Spark)
- HTML template
- ID3Info
- Image
- Image (Spark)
- ImageSnapshot
- itemRenderer
- JointStyle
- Label
- Label (Spark)
- Legend
- LegendItem
- LigatureLevel
- Line
- LinearGradientStroke
- LineScaleMode
- LinkBar
- LinkButton
- List
- List (Spark)
- Menu
- MenuBar
- Metadata
- MetadataEvent
- Model
- Mouse
- MouseCursor
- MouseEvent
- Move
- Namespace
- NavigatorContent (Spark)
- needsSWF
- NetConnection
- NetStream
- Nightly Builds
- NumberBaseRoundType
- NumberFormatter
- NumberValidator
- NumericCompare
- NumericStepper
- NumericStepper (Spark)
- ObjectProxy
- ObjectUtil
- paddingLeft
- paddingRight
- Panel
- Panel (Spark)
- Parallel
- Path
- PieChart
- PieSeries
- PieSeriesItem
- PopUpAnchor (Spark)
- PopUpButton
- PopUpManager
- ProgrammaticSkin
- ProgressBar
- PropertyChangeEvent
- QName
- RadialGradient
- RadioButton
- RadioButton (Spark)
- RadioButtonGroup
- RadioButtonGroup (Spark)
- Rect
- RegExp
- Regular Expressions
- Repeater
- RichEditableText
- RichText
- RichText (Spark)
- RichTextEditor
- Rotate
- Rotate3D (Spark)
- Scroller (Spark)
- Sequence
- setStyle()
- SimpleText
- SimpleText (Spark)
- skinClass
- Slider
- SliderEvent
- SolidColor
- SolidColorStroke
- Sort
- SortField
- Sound
- SoundEffect
- Spinner (Spark)
- SpriteVisualElement (Spark)
- StageDisplayState
- States
- StringUtil
- StringValidator
- StyleManager
- Styles
- SWFLoader
- SWFObject
- System
- SystemManager
- TabBar
- TabBar (Spark)
- TabNavigator
- TabStopFormat
- Text
- Text Layout Framework (TLF)
- TextArea
- TextArea (Spark)
- TextBox
- TextConverter
- TextEvent
- TextFlow
- TextFlowUtil
- TextFormat
- TextGraphic
- TextInput
- TextInput (Spark)
- TextLayoutFormat
- TextView
- Themes
- TileLayout
- TileList
- TileOrientation
- Timer
- TitleWindow
- TitleWindow (Spark)
- TLF
- ToggleButton (Spark)
- ToggleButtonBar
- ToolTip
- Transition
- Tree
- TruncationOptions
- UIComponent
- UIFTETextField
- Updater
- URLLoader
- URLRequest
- URLUtil
- URLVariables
- ValidationResultEvent
- Validator
- Validators
- VBox
- VDividedBox
- Vector
- VerticalLayout
- VerticalLayout (Spark)
- VGroup (Spark)
- Video
- VideoDisplay
- VideoElement
- VideoElement (Spark)
- VideoEvent
- VideoPlayer (Spark)
- VideoPlayerScrubBar
- ViewStack
- VScrollBar (Spark)
- VSlider
- VSlider (Spark)
- XML
- XMLList
- XMLListCollection
- ZipCodeValidator
- ZipCodeValidatorDomainType
- Zoom
-
Articles
- December 2010
- November 2010
- October 2010
- September 2010
- August 2010
- July 2010
- June 2010
- May 2010
- April 2010
- March 2010
- February 2010
- January 2010
- December 2009
- November 2009
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008
- February 2008
- January 2008
- December 2007
- November 2007
- October 2007
- September 2007
- August 2007
- July 2007
-
Meta


Yeah thx, its really usefull :D I’ve been posting on some forums how to load an xml file in flex 4 , and your guide is perfect! :)
One question, is this feature enabled today (4 November, 2009) ? I’m pretty sure I typed in flex 4 <xml and it didn't find anything, now it finds and
@divin3x,
Should work. I used yesterday’s build, I believe.
Peter
Hey Peter… I’m running into problems with the Spark List control and this example. If you use a dataprovider that you add children to, say with a button that calls addChild(), then after about the 7th time of adding a new child to the list, the list overwrites one of the children in addition to adding to the bottom of the list. I’ve checked the XML and it looks accurate (it’s not actually overwriting), but the list displays the children() of the XMLListCollection oddly. Any thoughts as to why this happens?
@Doug,
Do you have a small, reproducible test case? I’m not 100% sure what you mean by using
addChild()and how you’re adding items to the List data provider.If you think it’s a Flex bug, you can file it at http://bugs.adobe.com/flex/ and myself or somebody else can take a look.
Peter
Hey Peter… I was just checking this page for updates and saw your note. I will try to whip out a quick example test case. Would the best place to post it be here or on the bug that I filed?
I’d say the best place is probably in the official bug base, and then post the bug number here.
At least that way it will be officially tracked in JIRA (the bug base).
Thanks,
Peter
Bug created and example file attached to bug. The bug number is SDK-25486. It appears as if “appendChild” is losing the pointer to the end of the list somehow.
Just an update. I made an example file for that bug to show what the list is doing. It appears that it might not actually the list, but with XMLList.addChild (ala Collection elements) instead. Sorry for the confusion.
@Doug,
Not sure I really understand the issue (I’m more of an
XMLListCollection.addItem()and not anXMLList.appendChild()guy myself), but does this help at all?Peter
Sorry – am just now seeing your reply. It seems to get around the issue with addChild(). Thanks for suggesting the work-around!
Your examples are far more complex than what I’m trying to do, but I still can’t get it to work right. In Flex 3, you can have an HTTPService grab an XML file as e4x and set your dataprovider of the List to the node you wanted.
But trying to switch that to s:List completely fails and shows nothing, but without error:
From what I understand, that is because the e4x object being returned by HTTPService (even s:HTTPService) does not implment the IList interface.
Question being then: why the hell not?
Second: What the hell do I do instead? I’ve tried numerous other formats (default object format, XML format, Array, etc) and those do fail with errors. Usually along the lines of not being able to convert ObjectProxy into IList.
So far for me Flex 4 is a total bust. I thought the goal was to separate the skin from the components, but it seems other features were similarly stripped. I think it is quite obvious that if the mx: example above works, the s: example should also work. It is highly frustrating.
<code>
</code>
Trying to post the examples with the code tag, but it’s not working. I see someone else could do it, so here’s my try. I did this post with entity codes for lt and gt.
Hi,
Can we do the same with Adobe flex builder 3. If possible then please help me i am trying to load external XML file but not able to do that.
Thanks…..
@munaj ahmad,
It is definitely possible in Flex 3, the usual gotcha is whether you’re trying to use a node or attribute. You may have to try a
labelFieldof “@label” if you’re using an attribute, or “label” if you’re using nodes.Peter
its reaaly useful for me…. thanks yar
Thanks for the post. This really helped me out. I wasted a ton of time before finding your code.
Thanks again!
Peter,
Can you explain the difference between data binding and using Action Script?
I have an .mxml file with an external .xml file as in your example above. I compiled and the application works, but I need my “configuration” programmers to be able to change the data in the .xml file and see those updates in my application but it isn’t working.
What am I missing here?
Thanks.
Will
Did you get it done’?? i have the same problem i think, the nodes are saved but my combobox does not refresh when i call the httpservice =(
Hey thanks for reading me, i have a huge problem, i´m saving an xml file with fileStream method, after it’s saved, i call the httpService that was saved, but it does not return the last nodes I added, there’s any way i can refresh the httpService so i can show the last info i saved?? please I need this to be done by now, thabk you so much