PDA

View Full Version : Summing up nodeValues in flash with as3.



Tolo-Dex
April 4th, 2008, 10:49 PM
I need to know how to add up XML node values in flash such as the amount of funds and such to reach total values average values and such. *Desperately Needed* my nodes are.



<?xml version="1.0" encoding="UTF-8"?>
<row>
<colum>
<First>Clive</First>
<Last>Walters</Last>
<Business>Abundant Life Christian Ministries</Business>
<Address>70 Delta Park Blrd Brampton, Ontario L6T 5E9</Address>
<Amount>5000</Amount>
<Payment_Method>Master Card</Payment_Method>
<Payment_Plan>One Time</Payment_Plan>
<regDate>04-04-2008</regDate>
<expDate>04-04-2009</expDate>
</colum>
</row>

Flash code


package
{
import flash.display.MovieClip;
import flash.display.Loader;
import flash.events.Event;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.xml.*;

public class LoadingXML extends XMLDocument
{
private var my_root:MovieClip;

public function LoadingXML(m:MovieClip)
{
my_root=m;
this.loadXML();
}
private function loadXML():void
{
var loader:URLLoader=new URLLoader();
loader.addEventListener(Event.COMPLETE,completeHan dler);



var request:URLRequest=new URLRequest('Data.xml');
try
{
loader.load(request);
}
catch(error:Error)
{
trace('Impossibile caricare il documento.');
}
}

private function completeHandler(event:Event):void
{
var loader:URLLoader=URLLoader(event.target);
var result:XML=new XML(loader.data);
var myXML:XMLDocument=new XMLDocument();
myXML.ignoreWhite=true;
myXML.parseXML(result.toXMLString());
var node:XMLNode=myXML.firstChild;
var n:int=node.childNodes.length;
my_root.First=node.nodeName;
for(var i:int=0;i < n;i++)
{
var obj:Object=new Object();
var s:Number=node.childNodes[i].childNodes.length;
for(var j:int=0;j < s;j++)
{
if(i==0)
my_root.colonne_array.push(node.childNodes[i].childNodes[j].nodeName);
if(j==0)
obj.First=node.childNodes[i].childNodes[j].firstChild.nodeValue;
if(j==1)
obj.Last=node.childNodes[i].childNodes[j].firstChild.nodeValue;
if(j==2)
obj.Business=node.childNodes[i].childNodes[j].firstChild.nodeValue;
if(j==3)
obj.Address=node.childNodes[i].childNodes[j].firstChild.nodeValue;
if(j==4)
obj.Amount='$ '+node.childNodes[i].childNodes[j].firstChild.nodeValue;
var e:Number=node.childNodes[i].childNodes[j].firstChild.nodeValue;
trace(e);
if(j==5)
obj.Payment_Method=node.childNodes[i].childNodes[j].firstChild.nodeValue;
if(j==6)
obj.Payment_Plan=node.childNodes[i].childNodes[j].firstChild.nodeValue;
if(j==7)
obj.regDate=node.childNodes[i].childNodes[j].firstChild.nodeValue;
if(j==8)
obj.expDate=node.childNodes[i].childNodes[j].firstChild.nodeValue;
}
my_root.objects_array.push(obj);
}
my_root.creaFirst();
my_root.initDataGrid();
}
}
}
Please help or link me with help plz

And I do help others with the same problems, I hope this post would help others to.:)

Leo T

wvxvw
April 5th, 2008, 06:43 AM
var xml:XML = <table>
<row>
<colum>
<First>Clive</First>
<Last>Walters</Last>
<Business>Abundant Life Christian Ministries</Business>
<Address>70 Delta Park Blrd Brampton, Ontario L6T 5E9</Address>
<Amount>5000</Amount>
<Payment_Method>Master Card</Payment_Method>
<Payment_Plan>One Time</Payment_Plan>
<regDate>04-04-2008</regDate>
<expDate>04-04-2009</expDate>
</colum>
</row>
<row>
<colum>
<First>Clive</First>
<Last>Walters</Last>
<Business>Abundant Life Christian Ministries</Business>
<Address>70 Delta Park Blrd Brampton, Ontario L6T 5E9</Address>
<Amount>100.54</Amount>
<Payment_Method>Master Card</Payment_Method>
<Payment_Plan>One Time</Payment_Plan>
<regDate>04-04-2008</regDate>
<expDate>04-04-2009</expDate>
</colum>
</row>
</table>;
function sumXML(xml:XML):Number {
var r_num:Number = 0;
for each(var xl:XML in xml..Amount){
r_num += parseFloat(xl.text());
}
return Number(r_num.toFixed(2));
}
trace(sumXML(xml));

Tolo-Dex
April 5th, 2008, 07:21 PM
yes :book:that's really good sir, I see that your really skilled and didn't leave any comment just the code, i'm impressed BTW! Can I have your e-mail kind sir. I need ppl like you.

Leroy Thompson

wvxvw
April 6th, 2008, 02:34 AM
It's really not difficult ;) Glad it helped.
I've PMed my e-mail, just in case... %)