944,159 Members | Top Members by Rank

Ad:
Oct 18th, 2009
0

Actionscript 3 XML help

Expand Post »
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
xml Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 26
Solved Threads: 31
Posting Whiz
samarudge is offline Offline
354 posts
since May 2008
Oct 18th, 2009
0
Re: Actionscript 3 XML help
What exactly are you trying to do?

Get those variables into an xmllist or something?
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 18th, 2009
0
Re: Actionscript 3 XML help
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)
Reputation Points: 26
Solved Threads: 31
Posting Whiz
samarudge is offline Offline
354 posts
since May 2008
Oct 18th, 2009
0
Re: Actionscript 3 XML help
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.
Featured Poster
Reputation Points: 1536
Solved Threads: 431
Posting Expert
iamthwee is offline Offline
5,865 posts
since Aug 2005
Oct 19th, 2009
1

Solution

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!
Reputation Points: 167
Solved Threads: 239
Nearly a Posting Virtuoso
rajarajan07 is offline Offline
1,445 posts
since May 2008
Oct 19th, 2009
0
Re: Actionscript 3 XML help
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
Reputation Points: 26
Solved Threads: 31
Posting Whiz
samarudge is offline Offline
354 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Graphics and Multimedia Forum Timeline: Help me please
Next Thread in Graphics and Multimedia Forum Timeline: Display products





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC