This content has been marked as final. Show 2 replies
One thing to bear in mind is that although your data starts as XML, when you bring it in to your program using <mx:Model> it is being turned into a tree of untyped ActionScript objects. Even if you're using an external source with Model, it is being pulled in at compile time rather than runtime. This is being done in a way that is ignorant of any XML schema in use; it uses a very straightforward method of constructing the object tree. The way it constructs the tree, multiple children with the same name become an array but a single child just becomes an object (or rather an ObjectProxy, so you can listen for changes & use binding).
You can do more or less what you're already doing, but you have to be sensitive to this...check types. You might also skip creating an ArrayCollection if you're just using it to access the data. So, something like this:
var itemArray:Object = items.item[index].itemArray1;
if (itemArray is Array)
Note that you'll have the same problem if you have only one item (the array index will fail).
A bit of a nuisance, but it will work.
Alternatively, you could include the data as XML, using the <mx:XML> tag. Then you'd work with the data using XML methods. (Even there you have a choice between E4X format and legacy format).
In the absence of more details, my personal preference would probably be to use <mx:XML> with E4X, which is fairly powerful, but the E4X documentation isn't as strong as it could be. But different scenarios might make one approach better than the other.
Flex SDK team.
Thanks for the tip, I used your first suggestion (checking for array-ness) as a first step to getting my test code to work.
Now that it works, I have been looking into XML datatypes and was interested in knowing if there are any books out there that deal with datatype philosophy for the flex language. For instance, which collection class is better suited for which container class and why. It looks to me like XML and tree fit hand in hand.
I tried to apply XMLCollection to the AdvancedDataGrid with some rather interesting results. Heres what I did:
1) Changed the Mx:Model to Mx:XML (source being an external XML)
2) Pushed XMLModel.item into an XMLCollection that replaces the ArrayCollectionItem
3) Created an AdvancedDataGrid with the XMLCollection as the dataProvider and a column for itemfield1
So far so good, everything is working as expected. Now I replace the dataProvider with a groupingCollection dataProvider. I use itemfield1 as the groupingField.
Interestingly enough when I run the new code, the grouped column behaves more like a tree. The base is the grouping field I chose, but there are implicit sub-grouppings. In other words, after I open folder "a" I see a folder for ArrayItem and opening that shows a folder for ArrayItemField and opening that reveals the data in ArrayItemField. The results are to me, rather unexpected, but still rather amazing at the same time. I wonder if there is a good way to use this, perhaps a sortable tree?