PDA

View Full Version : XML object to ArrayCollection


kminev
07-03-2008, 05:16 PM
I need to fill an ArrayCollection object from XML object.

My result event method returns a plain string with xml structure as follows:

<NFS>
- <Trade>
<OrNo>0000000Y</OrNo>
<Date>6/10/2008 6:25:00 AM</Date>
<IP>xx.xxx.xx.xxx</IP>
<Ex>CBOT-A</Ex>
<A>Add</A>
<Trd>xxx</Trd>
<W2G>15</W2G>
<W2E>15</W2E>
<G2E>0</G2E>
</Trade>
</NFS>

From my result method I create new xml object and fill it with the result

For example: var xml:XML = new XML(event.result);

This is how I get the data from my web service call which comes as a string (event.result returns a string of xml)

The next step I just need to fill my ArrayCollection object with the data comming from the xml, but anything I tried didn't work for me.

Can anyone help? I think the problem is because my web service returns a string, but than the very same xml object binds to combo box so it doesn't make sense to me.

I would really appreciate if someone sheds some light on my issue.

box86rowh
07-03-2008, 06:35 PM
you are probably looking for an xmlListcollection?
If so, once you have created your cml object you can get that buy using xpath

NFS.Trade will return all trades in an xmlListcollection

drkstr
07-03-2008, 06:37 PM
Try this

pkg {

[Bindable]
public class TradeModel {

public function TradeModel( memetno:XML=null ) {
if(memento) {
setMemento(memento);
}
}

public var OrNo:String;
public var Date:String; //or convert to Date object in setMemento
public var IP:String;
public var Ex:String;
public var A:String;
public var Trd:String
public var W2G:Number;
public var W2E:Number;
public var G2E:Number;


public function setMemento( memento:XML ): void {
OrNo = memento.OrNo;
Date = memento.Date;
IP = memento.IP;
Ex = memento.Ex;
A = memento.A;
Trd = memento.Trd;
W2G = Number( memento.W2G );
W2E = Number( memento.W2E );
G2E = Number( memento.G2E );
}

}

}

var resultXML:XML = XML(event.result);
var trades:Array = [];
for each( var node:XML in resultXML.children() ) {
trades.push( new TradeModel(node) );
}

myArrayCollection = new ArrayCollection(trades);


There are a couple different ways to define your data model, but this is probably the quickest.

Best Regards,
~Aaron

kminev
07-03-2008, 08:19 PM
Thanks for the code snippet it is very helpful. However when I try to execute the following line I am getting an error:

for each( var node:XML in resultXML.children() )
{
//It happens here//trades.push(new TradeModel(node));
}
The error reads possibly undefined method.

I have included my package as an import and i don't see a reason why it shouldn't work.

Here is my package:
package TradeDataModel{

[Bindable]
public class TradeModel
{

public function TradeModel(memento:XML=null )
{
if(memento)
{
setMemento(memento);
}
}

public var OrNo:String;
public var Date:String; //or convert to Date object in setMemento
public var IP:String;
public var Ex:String;
public var A:String;
public var Trd:String
public var W2G:Number;
public var W2E:Number;
public var G2E:Number;


public function setMemento( memento:XML ): void
{
OrNo = memento.OrNo;
Date = memento.Date;
IP = memento.IP;
Ex = memento.Ex;
A = memento.A;
Trd = memento.Trd;
W2G = Number( memento.W2G );
W2E = Number( memento.W2E );
G2E = Number( memento.G2E );
}

}

}

Thank you.

drkstr
07-07-2008, 09:16 PM
Whenever you get that error when constructing a class, it means it is not imported correctly.

package TradeDataModel{
This is not the correct syntax. Is this a typo?

pkg {

public class TradeModel {
}
}

This means there should be the following file to import

SOURCE_ROOT/pkg/TradeModel.as

and imported like so:

import pkg.TradeModel;

However, I like to keep all my data model classes in their own directory.

pkg.models {

public class TradeModel {
}
}

..making the necessary adjustments elsewhere.


Best Regards,
~Aaron

kminev
07-08-2008, 02:39 PM
Thank you I got it to work. It was something funny I just added some get and set methods and that solved the problem oddly enough.