Groups | Blog | Home
all groups > flash actionscript > september 2007 >

flash actionscript : AS3 Loading XML and Display on Stage


Nancy - Adobe Comm. Expert
9/15/2007 5:08:41 PM
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

Nancy - Adobe Comm. Expert
9/16/2007 2:50:09 PM
It still doesn't display.

First I got an error that input_txt was undefined, so I defined the variable
and it doesn't throw errors now, but it also doesn't put anything on the
stage.

This is what I now have:

trace("XML Output");
trace("------------------------");
var weatherChildren:XMLList = weatherInput.cc.children();
var input_txt:TextField = new TextField;
for each (var info:XML in weatherChildren) {
input_txt.appendText(info);
}

I also tried putting a dynamic text field on the stage and naming it
input_txt and that doesn't do anything either.

I need to have things like

Temperature: thetemperature

where thetemperature is the dynamic number from the XML feed. The whole
feed is displaying in the output window .. what I can't do is get values
from that window to display on the stage.

Thanks for your help!
Nancy


Ardy15jan
9/16/2007 5:03:06 PM
[i]for each (var info:XML in weatherChildren) {
trace(info);
}[/i]

Try using "input_txt.appendText(info);" instead of "trace(info);"...
Nancy - Adobe Comm. Expert
9/16/2007 5:10:22 PM
this is frustrating ..

I had what I thought would do it .. but I keep getting this error ..

a conflict exists with definition tmpfield in namespace internal.


I have tried changing the name .. changing the definition .. changing the
movie .. changing everything and this keeps coming up no matter what I
change the definition name to. Why is this?

Thanks,
Nancy

Nancy - Adobe Comm. Expert
9/16/2007 10:03:18 PM
Thank you! I do have it working now .. however, your code is much cleaner
than mine so I'll study this thoroughly and learn. Thanks so much!

Nancy

[quoted text, click to view]

Nancy - Adobe Comm. Expert
9/16/2007 10:48:27 PM
Well .. finally all done .. and uploaded and it doesn't work online. I have
a hunch it's probably the crossdomain thingie I have read about .. ::sigh::

Thanks all for the help.
Nancy

Nancy - Adobe Comm. Expert
9/16/2007 11:03:24 PM
That was it .. all solved and working nicely on the site.

Thanks again, everyone!
Nancy

[quoted text, click to view]

SymTsb
9/17/2007 2:58:19 AM
When handling XML, displaying the data from a load (assuming you know the exact
structure) simply requires calling the name of the element using dot syntax.
The attached code is a very basic example of this. E4X has some other very
interesting features two of which I've tried to show since they tend to be the
most pertinent when handling XML.



import flash.display.*;
import flash.text.*;

var xmlString:String = "<gamescore><username>Mike Garcia</username><score
number='10716' /></gamescore>";
var xmlData:XML = new XML( xmlString );

var myTF:TextField = new TextField();
myTF.width = 250;
myTF.height = 20;

var myTF2:TextField = new TextField();
myTF2.width = 250;
myTF2.height = 20;
myTF2.y = 100;

// Display both text fields
// myTF shows how to assign the value of a text node to a text field
// myTF2 shows how to assign the value of an attribute to a text field
myTF.text = xmlData.username;
myTF2.text = xmlData.score.@number;
addChild(myTF);
addChild(myTF2);
SymTsb
9/17/2007 9:22:42 PM
AddThis Social Bookmark Button