Binding Complex XML to a DataGrid in Flex (Redux)

Most binding examples work with a flat XML structure. However, in most cases the format of the data will not work directly with Flex components. I was looking for a way to bind complex XML to a DataGrid in Flex 2. I ran across a post by Brandon Purcell dealing with the same subject but his post was a little out of date and no longer worked in Flex 2. There was also some talk on Flash Coders about complex XML and using the labelFunction to bind it to the DataGrid, but I could not find a complete example. So I created a working example using the similar data.

I ran into some difficulty at first when I placed the labelFunction inside the DataGridColumn. If you don’t pass column:DataGridColumn to the myLabelFunc, the DataGrid will not show up. You can view the example here, right-click to view the source code. For a detailed discussion, please visit Brandon’s original post.

-Mr

9 Comments

  1. Thank you so much – I’ve been struggling with this.
    My problem is that I have parsed my XML file into an ArrayCollection, and use the ArrayCollection as the dataProvider. Is there a way to do something similar with an ArrayCollection or do I need to keep my data as an XML object?
    Thanks anyway, this has got me on the right track at last.

  2. Elsa,

    I have a couple of questions. First, is your ArrayCollection bindable? Because it seems you could use the same exact binding method if you have already parsed out your XML into an Array Collection.

    You could try replacing in my example the following code

    with this

    Then parse your XML into a temp file, then assign it to the id xmlData. I am interested in the results, so if you want, you can send me a file to tinker with :). Check my “About” section for my email.

  3. Thanks,

    Yes the ArrayCollection is bindable. I got it working with your example and an ArrayCollection, so it must be something to do with the fact that my datagrid is sat in a custom component.
    I’m using get and set methods to pass the array into the component. I will have a bit more of an explore and see how far I get … thanks for the help so far.

  4. Great, glad you got it working. It seems like passing the ArrayCollection into the the custom component should work fine. Maybe you need a local var inside the component that is also Bindable to the DataGrid. I have experienced some issues with binding to components within states, using a stack seems to yield better results.

    – mr

  5. Wahoo! got it working. I’m too embarrassed to say why it didn’t work. One of those staring at the code too long moments I think. But over the moon now I’ve made some progress. Thanks again for your advice.

  6. I have had those moments too :D. I checked out the the FlexDeveloper.eu site, very interesting. It looks like a fledgling site, I hope it takes off. If you ever have a sample application up, I would like to check it out. Thanks!

  7. Utilizando o FLEX :
    Como transformar um XML em Arraycollection ?
    Im from Brasil lenguage(Portugues).pt-br

Comments are closed.