This content has been marked as final. Show 6 replies
If you have not set the HTTPService resultFormat property, Flex is automatically converting your xml into a structure of nested dynamic Objects. this is rarely desirable, but it does automatically create arrays from XMLLists, which enables you to use dot navigation as you are.
My advice would be to set resultFormat="e4x", which will give you true xml in your app. But you will have to change how you are using that data. You can use the XML directly, or wrap it in an XMLListCollection. This is much more flexible than the default conversion.
If you want to stick with the path you are on, you could try using mx:Model to wrap your XML. It performs the same conversion as the resultFormat="object"
Is this how I use the Model?
public function LoadKPIReport(DataXML:XML):void
ModelCtrl.source = DataXML;
ListBoxAC = new ArrayCollection();
ListBoxAC.addItem( ModelCtrl.Region.Market.source );
It doesn't run as I expected. And, I got the following message.
TypeError: Error #1009: Cannot access a property or method of a null object reference.
CAn anyone tell me what's wrong with my code?
What Tracy was saying was that mx:Model parses your XML in the same way as a HTTPService that doesn't have the resultFormat set. What this means is that you don't specify the resultFormat property of mx:Model - it doesn't have this property as it doesn't load the data at run-time, it actually compiles it into the application. So you can either put your XML directly into the model:
<mx:Model id="ModelCtrl" source="fileName" />
They are both equivalent.
Then something like your original function should work (hard to tell without seeing your XML):
public function setKPIReport():void
ListBoxAC = new ArrayCollection( ArrayUtil.toArray(ModelCtr.Region.Market));
This is a working example:
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="init()">
public function init():void
var ac:ArrayCollection = new ArrayCollection(ArrayUtil.toArray(MyEmployee.name));
trace("First name of first object is: " + ac.getItemAt(0).first);
Yes, excellent example, thank you dazweja.
I still advise e4x for the long run.
Thank you to Tracy & dazweeja. :)
Yes, I agree totally Tracy. If it's already an XML object, why not keep it that way and take advantage of all the great functionality provided by e4x? If you need to add collection functionality to an XML/XMLList object, use XMLListCollection - that's what it's there for. I honestly can't see any good reason to use ArrayCollection over XMLListCollection in situations where your data is an XML object.
Here's some good e4x links if anyone's interested: