Re: [Flashcoders] Simplify XML Call
From: Henrik Andersson he...@henke37.cjb.net To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Friday, August 12, 2011 1:43 PM Subject: Re: [Flashcoders] Simplify XML Call ActionScript 3 is a compiled language. There is no such thing as an eval function in as 3. Well, that's the crux of my problem, Henrik. How do I get around writing code that adds just the right number of item units without doing it as a string to evaluate or writing a switch statement that's obviously limited? Again, here's my pseudo-code: var j:String = xml.menu.item[whichItems[0]]; var k:String = new String(); var l:int = level; // I don't know this value in advance: it's passed to the class while (l--) { k += .item[whichItems[ + String(l) + ]]; } totalItems = eval(j + k); // eval() doesn't work in AS3, concept from Python TIA, John ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Simplify XML Call
Maybe Try var evalJ = eval(j); var evalK = eval(k); totalItems = evalJ + evalK; Evaluate the strings before you combine them? Karl On Aug 13, 2011, at 1:52 PM, John Polk wrote: From: Henrik Andersson he...@henke37.cjb.net To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Friday, August 12, 2011 1:43 PM Subject: Re: [Flashcoders] Simplify XML Call ActionScript 3 is a compiled language. There is no such thing as an eval function in as 3. Well, that's the crux of my problem, Henrik. How do I get around writing code that adds just the right number of item units without doing it as a string to evaluate or writing a switch statement that's obviously limited? Again, here's my pseudo-code: var j:String = xml.menu.item[whichItems[0]]; var k:String = new String(); var l:int = level; // I don't know this value in advance: it's passed to the class while (l--) { k += .item[whichItems[ + String(l) + ]]; } totalItems = eval(j + k); // eval() doesn't work in AS3, concept from Python TIA, John ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Karl DeSaulniers Design Drumm http://designdrumm.com ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Simplify XML Call
There is still no eval function. ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
Re: [Flashcoders] Simplify XML Call
From: Henrik Andersson he...@henke37.cjb.net To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Saturday, August 13, 2011 3:37 PM Subject: Re: [Flashcoders] Simplify XML Call There is still no eval function. I know there is no eval fn. Jason Merrill earlier wrote: You just need a recursive loop to do this. So I would write a function that handles each node level individually, adding to a class-level private property called something like, _totalItems. I tried googling [as3 recursive loop xml node] without much luck. Then I think I hit on what Jason was suggesting: var x:*; x = xml.menu.item[whichItems[0]]; var levelsLeft:int = level - 2; var q:int = 4; while(levelsLeft--) { x = x.item[whichItems[q]]; q--; } trace('xxx', x.item.length()); The problem is that for reasons that don't bear explaining I'm not ready to test this beyond the first level today :-} But I think it (or some tweak) should work (and I'll clean out that wildcard var). Thanks, John ___ Flashcoders mailing list Flashcoders@chattyfig.figleaf.com http://chattyfig.figleaf.com/mailman/listinfo/flashcoders
RE: [Flashcoders] Simplify XML Call
Then I think I hit on what Jason was suggesting: Not really. :) I think you're over complicating this. This is all I was suggesting you do from my original suggestion, it's pretty straightforward (this is a test case you could copy paste and run): //DUMMY DATA: var myXML:XML = data functionalarea type=amountKeying backgroundImage=images/background.png exercises exercise type=NoviceRDS keyingItems keyingItem fileURL=assets/images/simulations/ATC/atc_701201045.png invertedFileURL=myImages/myFileInverted1.png legible=true amount=78400 / keyingItem fileURL=myImages/myFile2.png invertedFileURL=myImages/myFileInverted2.png legible=false amount=743600 / keyingItem fileURL=myImages/myFile3.png invertedFileURL=myImages/myFileInverted3.png legible=true amount=3213212 / keyingItem fileURL=myImages/amountkeying/myFile4.png invertedFileURL=myImages/myFileInverted4.png legible=true amount=43242323 / keyingItem fileURL=myImages/myFile5.png invertedFileURL=myImages/myFileInverted5.png legible=true amount=78400 / keyingItem fileURL=myImages/myFile6.png invertedFileURL=myImages/myFileInverted6.png legible=false amount=342132 / keyingItem fileURL=myImages/myFile7.png invertedFileURL=myImages/myFileInverted7.png legible=true amount=78400 / /keyingItems /exercise exercise type=ExpertRDS keyingItems keyingItem fileURL=assets/images/simulations/ATC/atc_701201045.png invertedFileURL=myImages/myFileInverted1.png legible=true amount=78400 / keyingItem fileURL=myImages/myFile2.png invertedFileURL=myImages/myFileInverted2.png legible=false amount=743600 / keyingItem fileURL=myImages/myFile3.png invertedFileURL=myImages/myFileInverted3.png legible=true amount=3213212 / keyingItem fileURL=myImages/amountkeying/myFile4.png invertedFileURL=myImages/myFileInverted4.png legible=true amount=43242323 / keyingItem fileURL=myImages/myFile5.png invertedFileURL=myImages/myFileInverted5.png legible=true amount=78400 / keyingItem fileURL=myImages/myFile6.png invertedFileURL=myImages/myFileInverted6.png legible=false amount=342132 / keyingItem fileURL=myImages/myFile7.png invertedFileURL=myImages/myFileInverted7.png legible=true amount=78400 / /keyingItems /exercise /exercises /functionalarea /data //RECURSIVE FUNCTION: var totalItems:uint = 0; function countItems(xmlNode:XML):void { var xmlChildren:XMLList = xmlNode.children(); if(xmlChildren.length() 0) { for each (var xmlNode:XML in xmlChildren) { totalItems++; countItems(xmlNode); //The recursive call } } } countItems(myXML); //RESULT: trace(totalItems);//Traces 20 - there are 20 nodes in the given XML from the root down. Instead of the root, you could start farther in too if you wanted, i.e.: var exercisesXML:XML = myXML.functionalarea[0].exercises[0]; countItems(exercisesXML); trace(totalItems); //traces 18 - there are 18 nodes from that level down. If that's not exactly what you need, it would seem the general concept could be modified to fit your needs. Hope that helps, Jason Merrill Instructional Technology Architect II Bank of America Global Learning ___ -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of John Polk Sent: Saturday, August 13, 2011 5:58 PM To: Flash Coders List Subject: Re: [Flashcoders] Simplify XML Call From: Henrik Andersson he...@henke37.cjb.net To: Flash Coders List flashcoders@chattyfig.figleaf.com Sent: Saturday, August 13, 2011 3:37 PM Subject: Re: [Flashcoders] Simplify XML Call There is still no eval function. I know there is no eval fn. Jason Merrill earlier wrote: You just need a recursive loop to do this. So I would write a function that handles each node level individually, adding to a class-level private property called something like, _totalItems. I tried googling [as3 recursive loop xml node] without much luck. Then I think I hit on what Jason was suggesting: var x:*; x = xml.menu.item[whichItems[0]];
RE: [Flashcoders] Simplify XML Call
And here's another recursive function example for XML, slightly different, with a more complex structure, showing how to get all values from some attributes in an XML file (in this case, firstName and lastName), no matter where or how deep they lie: var peopleXML:XML = people person firstName=Bob lastName=Smith person firstName=Timmy lastName=Smith / person firstName=Jenny lastName=Jones person firstName=Sal lastName=Stephens / /person person firstName=Marcia lastName=Marquez person firstName=Julio lastName=Rogers/ /person /person person firstName=Tom lastName=Williams person firstName=Mary lastName=Jones / person firstName=Albert lastName=Denniston person firstName=Barney lastName=Elmington / person firstName=Campo lastName=Fatigua person firstName=Harpo lastName=Oprah/ /person person firstName=Hugo lastName=Boss person firstName=Benny lastName=Elkins/ person firstName=Sheri lastName=Downing/ /person /person /person person firstName=Marcia lastName=Marquez person firstName=Manny lastName=Peterson/ /person person firstName=Joe lastName=Merritt/ /people; function recurseXML(xml:*):void { var xmlList:XMLList = xml.children(); for each (var currentNode:* in xmlList) { trace(currentNode.@firstName+ +currentNode.@lastName); if(currentNode.children()) recurseXML(currentNode); } } recurseXML(peopleXML); //Traces: Bob Smith Timmy Smith Jenny Jones Sal Stephens Marcia Marquez Julio Rogers Tom Williams Mary Jones Albert Denniston Barney Elmington Campo Fatigua Harpo Oprah Hugo Boss Benny Elkins Sheri Downing Marcia Marquez Manny Peterson Joe Merritt Jason Merrill Instructional Technology Architect II Bank of America Global Learning ___ -Original Message- From: flashcoders-boun...@chattyfig.figleaf.com [mailto:flashcoders-boun...@chattyfig.figleaf.com] On Behalf Of Merrill, Jason Sent: Saturday, August 13, 2011 9:58 PM To: Flash Coders List Subject: RE: [Flashcoders] Simplify XML Call Then I think I hit on what Jason was suggesting: Not really. :) I think you're over complicating this. This is all I was suggesting you do from my original suggestion, it's pretty straightforward (this is a test case you could copy paste and run): //DUMMY DATA: var myXML:XML = data functionalarea type=amountKeying backgroundImage=images/background.png exercises exercise type=NoviceRDS keyingItems keyingItem fileURL=assets/images/simulations/ATC/atc_701201045.png invertedFileURL=myImages/myFileInverted1.png legible=true amount=78400 / keyingItem fileURL=myImages/myFile2.png invertedFileURL=myImages/myFileInverted2.png legible=false amount=743600 / keyingItem fileURL=myImages/myFile3.png invertedFileURL=myImages/myFileInverted3.png legible=true amount=3213212 / keyingItem fileURL=myImages/amountkeying/myFile4.png invertedFileURL=myImages/myFileInverted4.png legible=true amount=43242323 / keyingItem fileURL=myImages/myFile5.png invertedFileURL=myImages/myFileInverted5.png legible=true amount=78400 / keyingItem fileURL=myImages/myFile6.png invertedFileURL=myImages/myFileInverted6.png legible=false amount=342132 / keyingItem fileURL=myImages/myFile7.png invertedFileURL=myImages/myFileInverted7.png legible=true amount=78400 / /keyingItems /exercise exercise type=ExpertRDS keyingItems keyingItem fileURL=assets/images/simulations/ATC/atc_701201045.png invertedFileURL=myImages/myFileInverted1.png legible=true amount=78400 / keyingItem fileURL=myImages/myFile2.png invertedFileURL=myImages/myFileInverted2.png legible=false amount=743600 / keyingItem fileURL=myImages/myFile3.png invertedFileURL=myImages/myFileInverted3.png legible=true amount=3213212 / keyingItem fileURL=myImages/amountkeying/myFile4.png invertedFileURL=myImages/myFileInverted4.png legible=true amount=43242323 / keyingItem