XML And The Flex LinkBar's DataProvider Attribute

I was recently working on a Flex project where I wanted to use the LinkBar component (1). However, the data I needed to use that included the label values for the LinkBar's LinkButtons was in an XML file. As best I can tell, the LinkBar component is one of the few Flex components whose dataProvider attribute doesn't accept XMLList or XMLListCollection. According to reference 2, the dataProvider value for a LinkBar component must either be a ViewStack component or an array.

My original plan (which I've done before) was to loop over each repeating node in my XML and create an object with fields for each node's child elements. For example:


linkData = new ArrayCollection();

for each (var item:XML in _feeds ) {

//create an Object that we can add to our Array
var _itemObj:Object = new Object();

//need the toString to get the node's text as a String
//so that sets the property type for the _itemObj's properties
_itemObj.label = item.label.toString();
_itemObj.feed = item.feed.toString();
_itemObj.newsTitle = item.newsTitle.toString();

linkData.addItem( _itemObj );

} //end for

However, I'm reading Marco Casario's book Flex Solutions (5) and looking at one of his code examples I noticed that you can specify the resultFormat value for the mx:HTTPService component (3,4) to be "object". When you specify "object" as the value for the resultFormat, Flex apparently returns the XML but then parses the XML into a collection of Objects. For each repeating node an Object is created and added to the collection (4). You can then cast that collection to an ArrayCollection object and use that as the LinkBar's dataProvider.

Previously, whenever I'd used mx:HTTPService to get XML data into a Flex project I'd always set the resultFormat value to "e4x" so I could manipulate the result returned as XML. But after seeing the example in Flex Solutions when Marco uses mx:HTTPService with a resultFormat value of "object", I did some research. The default value for resultFormat is actually "object" (4). Even if the data being read by mx:HTTPService is XML you can still use "object" as the resultFormat. I just never made that connection and actually had forgotten that "object" was one of the values that can be used for the resultFormat attribute.

You can see a demonstration of using mx:HTTPService with a resultFormat value of "object" to read in XML, create a collection of Objects, and then use that collection as the dataProvider for the LinkBar by opening this Flex application (right click to view the source code). If you compare the XML file to the data returned by the mx:HTTPService you'll see that the XML was parsed into an Object with a field named channel (the root node of the XML). The channel field consists of an item field, which is a collection of Objects, one for each item node in the original XML. So I can cast the item field (event.result.channel.item) to an ArrayCollection and use that ArrayCollection as the dataProvider for the LinkBar control.

Side Note

I've worked my way through chapter 6 of Marco Casario's Flex Solutions book. So far, I've really found the book useful. Each chapter is filled with several complete code examples (which can be downloaded from the book's website) and good explanations of how each solution works. The example solutions range from basic to advanced. I enjoy studying other developer's code and the code examples in this book are easy to follow. It's especially rewarding to be able to study someone else's code and learn something new that I can apply to my own projects.

References

  1. Flex 3 Language Reference - LinkBar
  2. Flex 3 Language Reference - LinkBar dataProvider
  3. Flex 3 Language Reference - mx:HTTPService
  4. Flex 3 Language Reference - resultFormat
  5. Flex Solutions, Essential Techniques for Flex 2 and 3 Developers, Marco Casario, 2007
  6. Demo Application - Returning A Collection of Objects From An XML File

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Hello Bruce,
I am looking for an working example for this functionality. Below is my sample XML format .

<photos>
   <image filename="nature18" photographer="Vera Drake" category="nature">
      <desc>Crab apple trees in bloom.</desc>
   </image>
   <image filename="animals03" photographer="Emily Kim" category="animals">
      <desc>Chubby donkey looking for a friend.</desc>
   </image>
   <image filename="people08" photographer="Lisa Traylor" category="people">
      <desc>Tulle veil with pearl accents.</desc>
   </image>
</photos>

Can you kindly tell me how can I add the "filename" attribute to the linkbar.
Thank you in advance.
-Nish
http://www.vebguru.com
# Posted By Flash Developer | 5/10/08 12:24 PM
Hi Bruce
In my below xml I want to get the node name "employee". Currently I am getting that through "event.result.firstChild.nodeName". I am getting that as a string. But I want it as a XMLNode.

My XML is:

<employees>
   <employee id="1">
      <name>Girija Sankar</name>
<phone>555-219-2270</phone>
<email>[email protected]</email>
<empId>7359</empId>
   </employee>
<employee id="2">
      <name>Kishor</name>
<phone>555-219-2012</phone>
<email>[email protected]</email>
<empId>7410</empId>
</employee>
</employees>

Thanks and Regards
Girija Sankar
# Posted By Girija Malla Samanta | 6/16/08 1:50 AM
Bruce,

Maybe you can help me. I have the following code and I want to access the boolean of the saved_item (checkbox) in the XMLList. I don't get an error, but I don't see true/false. This field in not in my XML document coming back via httpservice.

TIA,
Pat


<mx:DataGridColumn headerText="Saved" dataField="saved_item" width="60" >
<mx:itemRenderer>
<mx:Component>
<mx:CheckBox />
</mx:Component>
</mx:itemRenderer>
</mx:DataGridColumn>
# Posted By Patrick Whittingham | 1/6/09 9:39 AM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner