views:

155

answers:

1

I want to pass external XML a variable. How do I do this?

WHAT I'M AFTER
- update my variable with COUNT XML

WHAT I'M NOT GETTING
- The integer to String values
- How to pass XML to a variable

link http://videodnd.weebly.com/

time.xml

<?xml version="1.0" encoding="utf-8"?>
<SESSION>
    <COUNT TITLE="starting position">-77777</COUNT>
</SESSION>

xml.fla

//VARIABLES
/*CHANGE TO COUNT
MyString or count, I don't know if it was necessary to go from int to String 
*/
var myString:String = "";       
var count:int = int(myString);
    trace(count);

//LOAD XML
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("time.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

//PARSE XML
function processXML(e:Event):void {
    myXML = new XML(e.target.data);
    trace(myXML.COUNT.*);
    trace(myXML);

//TEXT 
var text:TextField = new TextField(); 
    text.text = myXML.COUNT.*; 
    addChild(text);
}

output window 'traces to the output window correctly'

//zero should read -77777 if tracing correctly
0
-77777
<SESSION>
  <COUNT TITLE="starting position">-77777</COUNT>
</SESSION>

errors
coercion errors and null references with anything I attempt.

+1  A: 

A little fuzzy on what you're asking, but here is how you can pull the data from the XML as either a String or integer:

//VARIABLES
var myString:String = "";       
var count:int = 0;

//LOAD XML
var myXML:XML;
var myLoader:URLLoader = new URLLoader();
myLoader.load(new URLRequest("time.xml"));
myLoader.addEventListener(Event.COMPLETE, processXML);

//PARSE XML
function processXML(e:Event):void {
    myXML = new XML(e.target.data);
    trace(myXML.COUNT.text()); //-77777

    //grab the data as a string
    myString = myXML.COUNT.text();

    //grab the data as an int
    count = int(myXML.COUNT.text());

    trace("String: ", myString);
    trace("Int: ", count);
    trace(count - 1); //just to show you that it's a number that you can do math with (-77778)

    //TEXT 
    var text:TextField = new TextField(); 
    text.text = myString; 
    addChild(text);
}
walpolea
Bravo! My question was awful, but this is just what I was looking for.
VideoDnd
http://www.wuup.co.uk/as3-quick-tips-string-to-number-conversion-and-vice-versa/
VideoDnd