Hi, I am trying to retrieve the contents of my XML file and use the variables later on in the script;
I have the following actionscript code

// Get XML Vars
var xml:XML;
var urlLoader = new URLLoader();
var songdataurl:String = '0';
urlLoader.addEventListener(Event.COMPLETE,onXMLLoaded);
urlLoader.load(new  URLRequest("data.xml"));

function onXMLLoaded(e:Event):void{
       xml =  new XML(e.target.data);
	   GetSongData();
}
function GetSongData():void{
       songdataurl = xml..song[0];
}
trace(songdataurl);

//Use songdataurl in script

and the following is the contents of data.xml

<?xml version="1.0"  encoding="UTF-8"?>
<songs>
	<song  title="A Song" artist="Joe Blogs" album="An Album">/getsong/?ID=12345&Hash=12345678&IP=0.0.0.0</song>
</songs>

I'm trying to get the contents of the <song></song> tag and use it later on in my script.

Thanx,
Sam

Recommended Answers

All 5 Replies

Member Avatar for iamthwee

What exactly are you trying to do?

Get those variables into an xmllist or something?

This is the first time I have ever used AS3, I just want the variable to be available further on in the script (outside onXMLLoaded function)

Member Avatar for iamthwee

I don't know if this is the best practise. It probably isn't but you could declare a global variable at the very top of the code then assign it later.

Member Avatar for rajarajan2017

Mr samarudge,

Your code is written in a right way!, and It works. But whats the problem is before reading your xml, the trace statement is executed once. You should give time to load your xml and after that only have to use trace statement. Thats the problem!.

you can test this by using the below code.

var songdataurl:String;
var xml:XML;
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE,onXMLLoaded);
urlLoader.load(new  URLRequest("data.xml"));

function onXMLLoaded(e:Event):void{
       xml =  new XML(e.target.data);
	   GetSongData();
}
function GetSongData():void{
       songdataurl = xml..song[0];
}

var timer:Timer=new Timer(500);
timer.addEventListener(TimerEvent.TIMER,process);
timer.start();
function process(e:TimerEvent):void
{
trace(songdataurl);
}

Hope this helps!

commented: Great response, accurate code +2

Mr samarudge,

Your code is written in a right way!, and It works. But whats the problem is before reading your xml, the trace statement is executed once. You should give time to load your xml and after that only have to use trace statement. Thats the problem!.

you can test this by using the below code.

var songdataurl:String;
var xml:XML;
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE,onXMLLoaded);
urlLoader.load(new  URLRequest("data.xml"));

function onXMLLoaded(e:Event):void{
       xml =  new XML(e.target.data);
	   GetSongData();
}
function GetSongData():void{
       songdataurl = xml..song[0];
}

var timer:Timer=new Timer(500);
timer.addEventListener(TimerEvent.TIMER,process);
timer.start();
function process(e:TimerEvent):void
{
trace(songdataurl);
}

Hope this helps!

GENIUS =D
Thanx, marked solved and rep added :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.