views:

59

answers:

2

There's an variable in my swf I want to receive XML. It's an integer value in the form of an equation. How do I receive the XML value for 'formatcount'?

My Variable

//Variable I want to grab XML<br>
//formatcount=int('want xml value to go here');

formatcount=int(count*count/100);

Path

formatcount = myXML.FORMATCOUNT.text()

My XML

<?xml version="1.0" encoding="utf-8"?>
   <SESSION>
      <TIMER TITLE="speed">1000</TIMER>
      <COUNT TITLE="starting position">10000</COUNT>
      <FORMATCOUNT TITLE="ramp">count*count/1000</FORMATCOUNT>
</SESSION>
+1  A: 

You will need to write or use a parser that will be able to parse and evaluate your expression:

Check for example this evaluator of expression:

Based on the above lib (not tested):

import it.sephiroth.expr.CompiledExpression;
import it.sephiroth.expr.Parser;
import it.sephiroth.expr.Scanner;
import flash.display.Sprite;


public class Example extends Sprite
{
  public function Example() {
    super();

    var myXML:XML=<SESSION>
      <TIMER TITLE="speed">1000</TIMER>
      <COUNT TITLE="starting position">10000</COUNT>
      <FORMATCOUNT TITLE="ramp">count*count/1000</FORMATCOUNT>
    </SESSION>;

    var expression: String = myXML.FORMATCOUNT.toString();
    var scanner: Scanner = new Scanner( expression );
    var parser: Parser = new Parser( scanner );

    var compiled: CompiledExpression = parser.parse();

    var count:Number=Number(myXML.COUNT.toString());

    var context: Object = {
      count:count
    };

    var formatcount:Number = compiled.execute( context );
  }
}
Patrick
Cool. Is the expression evaluator hard to use. I don't see a lot of documentation.
VideoDnd
@VideoDnd No i don't see any difficulty, but that's my point of view ;)
Patrick
+1  A: 

You seem to be trying to do a calculation inside your XML file, and while you could be trying to read in an equation from XML, I don't see why either needs to be done at the level of the XML file itself. It'd be much easier to just read in values from the XML file, and do any calculations in your AS.

If you've read the value of <COUNT> into a variable in your code (I presume through myXML.COUNT), then just do:

formatcount = count*count / 10000;

Where count is:

var count:Number = myXML.COUNT;

As is usual with your questions, I'm not sure if I'm answering your actual question or if I've misinterpreted it. Good luck anyway.

debu
Thanks, that straightened me out
VideoDnd