Question
AS3 Loading XML and Display on Stage
I haven't done much with AS3 .. just simple Flex stuff, but I
have to load
XML from an external URL and discovered this was much easier to get what I
needed with AS3. My problem is the XML loads and I can trace till the cows
come home, but I can't get the code right to display the information on the
stage.
Here is the loading code:
import flash.net.*;
import flash.events.*;
import flash.display.*;
import flash.geom.*;
import flash.ui.*;
import flash.utils.*;
import flash.text.*;
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("externalURLremoved"));
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
ParseWeather(xmlData);
}
function ParseWeather(weatherInput:XML):void {
trace("XML Output");
trace("------------------------");
var weatherChildren:XMLList = weatherInput.cc.children();
for each (var info:XML in weatherChildren) {
trace(info);
}
}
All the data displays in the output window perfectly. But I need to display
values from the XML on the stage. With AS2, I would have used the variable
box in the property inspector, but of course, this doesn't work in AS3. I
can display text values like this:
var input_txt:TextField = new TextField();
input_txt.x = 105;
input_txt.y = 35;
input_txt.width = 530;
input_txt.height = 156;
addChild(input_txt);
input_txt.text = "Hello World";
but I can't figure out how to substitute a value from the XML for the text.
All the sample I have found for loading XML stop with the trace statement in
the output window. I haven't found any that take that through to the stage
and I have looked online for hours and in every book that I have.
I thought it would be something like
input_txt.text=(info.tmp);
but that throws errors. Everything I have tried throws errors.
Any advice?
Thanks!
Nancy
XML from an external URL and discovered this was much easier to get what I
needed with AS3. My problem is the XML loads and I can trace till the cows
come home, but I can't get the code right to display the information on the
stage.
Here is the loading code:
import flash.net.*;
import flash.events.*;
import flash.display.*;
import flash.geom.*;
import flash.ui.*;
import flash.utils.*;
import flash.text.*;
var xmlLoader:URLLoader = new URLLoader();
var xmlData:XML = new XML();
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
xmlLoader.load(new URLRequest("externalURLremoved"));
function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
ParseWeather(xmlData);
}
function ParseWeather(weatherInput:XML):void {
trace("XML Output");
trace("------------------------");
var weatherChildren:XMLList = weatherInput.cc.children();
for each (var info:XML in weatherChildren) {
trace(info);
}
}
All the data displays in the output window perfectly. But I need to display
values from the XML on the stage. With AS2, I would have used the variable
box in the property inspector, but of course, this doesn't work in AS3. I
can display text values like this:
var input_txt:TextField = new TextField();
input_txt.x = 105;
input_txt.y = 35;
input_txt.width = 530;
input_txt.height = 156;
addChild(input_txt);
input_txt.text = "Hello World";
but I can't figure out how to substitute a value from the XML for the text.
All the sample I have found for loading XML stop with the trace statement in
the output window. I haven't found any that take that through to the stage
and I have looked online for hours and in every book that I have.
I thought it would be something like
input_txt.text=(info.tmp);
but that throws errors. Everything I have tried throws errors.
Any advice?
Thanks!
Nancy