Actionscript 3 XML help

Thread Solved

Join Date: May 2008
Posts: 231
Reputation: samarudge is an unknown quantity at this point 
Solved Threads: 19
samarudge samarudge is offline Offline
Posting Whiz in Training

Actionscript 3 XML help

 
0
  #1
Oct 18th, 2009
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
Graphics and Multimedia Syntax (Toggle Plain Text)
  1. // Get XML Vars
  2. var xml:XML;
  3. var urlLoader = new URLLoader();
  4. var songdataurl:String = '0';
  5. urlLoader.addEventListener(Event.COMPLETE,onXMLLoaded);
  6. urlLoader.load(new URLRequest("data.xml"));
  7.  
  8. function onXMLLoaded(e:Event):void{
  9. xml = new XML(e.target.data);
  10. GetSongData();
  11. }
  12. function GetSongData():void{
  13. songdataurl = xml..song[0];
  14. }
  15. trace(songdataurl);
  16.  
  17. //Use songdataurl in script

and the following is the contents of data.xml
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <songs>
  3. <song title="A Song" artist="Joe Blogs" album="An Album">/getsong/?ID=12345&Hash=12345678&IP=0.0.0.0</song>
  4. </songs>
I'm trying to get the contents of the <song></song> tag and use it later on in my script.

Thanx,
Sam
My Blog, Life and everything that matters to me - SamRudge.co.uk

2x Macbook Pro's, 1x Mac Pro, 1x iMac, 2x Macbook's running Fedora linux - In conclusion, I hate windows =)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert
 
0
  #2
Oct 18th, 2009
What exactly are you trying to do?

Get those variables into an xmllist or something?
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 231
Reputation: samarudge is an unknown quantity at this point 
Solved Threads: 19
samarudge samarudge is offline Offline
Posting Whiz in Training
 
0
  #3
Oct 18th, 2009
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)
My Blog, Life and everything that matters to me - SamRudge.co.uk

2x Macbook Pro's, 1x Mac Pro, 1x iMac, 2x Macbook's running Fedora linux - In conclusion, I hate windows =)
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 5,264
Reputation: iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold iamthwee is a splendid one to behold 
Solved Threads: 377
Featured Poster
iamthwee's Avatar
iamthwee iamthwee is offline Offline
Posting Expert
 
0
  #4
Oct 18th, 2009
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.
*Voted best profile in the world*
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 150
Reputation: rajarajan07 is on a distinguished road 
Solved Threads: 19
rajarajan07's Avatar
rajarajan07 rajarajan07 is offline Offline
Junior Poster

Solution

 
1
  #5
Oct 19th, 2009
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.

Graphics and Multimedia Syntax (Toggle Plain Text)
  1. var songdataurl:String;
  2. var xml:XML;
  3. var urlLoader:URLLoader = new URLLoader();
  4. urlLoader.addEventListener(Event.COMPLETE,onXMLLoaded);
  5. urlLoader.load(new URLRequest("data.xml"));
  6.  
  7. function onXMLLoaded(e:Event):void{
  8. xml = new XML(e.target.data);
  9. GetSongData();
  10. }
  11. function GetSongData():void{
  12. songdataurl = xml..song[0];
  13. }
  14.  
  15. var timer:Timer=new Timer(500);
  16. timer.addEventListener(TimerEvent.TIMER,process);
  17. timer.start();
  18. function process(e:TimerEvent):void
  19. {
  20. trace(songdataurl);
  21. }

Hope this helps!
Thanks & Regards,
RajaRajan. R

trulyraja2009@yahoo.in
Reply With Quote Quick reply to this message  
Join Date: May 2008
Posts: 231
Reputation: samarudge is an unknown quantity at this point 
Solved Threads: 19
samarudge samarudge is offline Offline
Posting Whiz in Training
 
0
  #6
Oct 19th, 2009
Originally Posted by rajarajan07 View Post
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.

Graphics and Multimedia Syntax (Toggle Plain Text)
  1. var songdataurl:String;
  2. var xml:XML;
  3. var urlLoader:URLLoader = new URLLoader();
  4. urlLoader.addEventListener(Event.COMPLETE,onXMLLoaded);
  5. urlLoader.load(new URLRequest("data.xml"));
  6.  
  7. function onXMLLoaded(e:Event):void{
  8. xml = new XML(e.target.data);
  9. GetSongData();
  10. }
  11. function GetSongData():void{
  12. songdataurl = xml..song[0];
  13. }
  14.  
  15. var timer:Timer=new Timer(500);
  16. timer.addEventListener(TimerEvent.TIMER,process);
  17. timer.start();
  18. function process(e:TimerEvent):void
  19. {
  20. trace(songdataurl);
  21. }

Hope this helps!
GENIUS =D
Thanx, marked solved and rep added
My Blog, Life and everything that matters to me - SamRudge.co.uk

2x Macbook Pro's, 1x Mac Pro, 1x iMac, 2x Macbook's running Fedora linux - In conclusion, I hate windows =)
Reply With Quote Quick reply to this message  
Reply

Tags
actionscript3, flash, xml

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Graphics and Multimedia Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC