Flash Gallery | Flash Templates | Flash Menu | Flash Design | Flash Audio & Video

flash page flip

Actionscript 3.0 video tutorials

+ Reply to Thread
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 15

Thread: Loading external XML files with Flash CS3

  1. #1
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    Loading external XML files with Flash CS3

    flash templates
    With Actionscript 3.0 the XML Class seems to have deeply changed.
    The XML class has been moved to the package flash.xml and its name has been changed to XMLDocument to avoid the conflict with the new XML Class, which has been enriched with the ECMAScript for XML.
    I?ll begin with saying that the load() and onLoad() methods of AS 2.0 have been removed, therefore we now use the URLLoader and URLRequest classes to load an external XML file.
    You add a listener to the instance of URLLoader and at the COMPLETE event, the XML parsing begins, hence you pass the data of the URLLoader?s instance to a new instance of the XML class, then you create a new instance of the XMLDocument class, which helps parsing the XML instance.
    It?s easier done than said, so let?s see the juice:

    This is the XML file ( very simple just to understand the example ):
    HTML Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <setting>
    	<parameters color="0xFFFFFF" name="Filippo" gen="male" age="34" picturePath="filippo.jpg">
    	</parameters>
    </setting>
    And this is the class I?ve built:
    Code:
    package
    {
    	import flash.display.Loader;
    	import flash.display.MovieClip;
    	import flash.events.*;
    	import flash.net.URLLoader;
    	import flash.net.URLRequest;
    	import flash.xml.*;
    	
    	public class LoadingXML extends MovieClip
    	{
    		public function LoadingXML()
    		{
    			this.loadXML();
    		}
    		private function loadXML():void
    		{
    			var loader:URLLoader=new URLLoader();
    			loader.addEventListener(Event.COMPLETE,completeHandler);
    		
    			var request:URLRequest=new URLRequest('setting.xml');
    			try 
    			{
    				loader.load(request);
    			} 
    			catch(error:Error) 
    			{
    				trace('Unable to load requested document.');
    			}
    		}
    		private function completeHandler(event:Event):void
    		{
    			var loader:URLLoader=URLLoader(event.target);
    			var result:XML=new XML(loader.data);
    			var myXML:XMLDocument=new XMLDocument();
    			myXML.ignoreWhite=true;
    			myXML.parseXML(result.toXMLString());
    			var node:XMLNode=myXML.firstChild;
    			trace('Colore= '+node.firstChild.attributes['color']);
    			trace('Nome= '+node.firstChild.attributes['name']);
    			trace('Genere= '+node.firstChild.attributes['gen']);
    			trace('Età= '+node.firstChild.attributes['age']);
    			trace('Path immagine= '+node.firstChild.attributes['picturePath']);
    		}
    	}
    }
    And this is the resulting output:
    Colore= 0xFFFFFF
    Nome= Filippo
    Genere= male
    Età= 34
    Path immagine= filippo.jpg
    See you next !

  2. #2
    Member Flash Addict gwulfwud is on a distinguished road
    Join Date
    Nov 2007
    Posts
    59
    Rep Power
    3

    Re: Loading external XML files with Flash CS3

    hey flep. im trying to use this tut to implement xml with my audio player. im having problems though, after i was able to insert the xml im having this error on my buttons..its a 1120: Access of undefined property ...

    here's my code btw

    Code:
    package com.externalFiles
    {
            
        import flash.display.MovieClip;
        import flash.media.Sound;
        import flash.media.SoundChannel;
        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.events.TimerEvent;
        import flash.net.URLRequest;
        import flash.net.URLLoader;
        import flash.xml.*;
        
        public class gwPlayer extends MovieClip
        {        
            public var url:Array;
            
            private var _channel:SoundChannel;
            private var _sound:Sound;
            private var _playing:Boolean=false;
            //initialize position of the sound (kung sang segundo na ung tugtog)
            private var _sposition:int=0;
            
            public function gwPlayer()
            {
                initVar();
                initButtons();
                initSound();
            }
            
            //simulan ang variables for multiple songs
            public function initVar():void
            {
                url=new Array();
                this.loadXML();
            }
            
            //iload ang xml document
            public function loadXML():void
            {
                var loader:URLLoader=new URLLoader();
                loader.addEventListener(Event.COMPLETE,loadComplete);
                
                var requestXML:URLRequest=new URLRequest('gwXML.xml');
                try
                {
                    loader.load(requestXML);
                }
                catch(error:Error)
                {
                    trace('Cannot find the xml file. Please contact the system administrator.');
                }
            }
            
            public function loadComplete():void
            {
                var loader:URLLoader=URLLoader(event.target);
                var result:XML=new XML(loader.data);
                var myXML:XMLDocument=new XMLDocument();
                myXML.ignoreWhite=true;
                myXML.parseXML(result.toXMLString());
                var node:XMLNode=myXML.firstChild;
                trace('title is '+node.firstChild.attributes['title']);
            }
                    
            //initialize buttons
            public function initButtons():void
            {
                playSound.addEventListener(MouseEvent.MOUSE_OVER,chPlayImage);
                pauseSound.addEventListener(MouseEvent.MOUSE_OVER,chPauseImage);
                stopSound.addEventListener(MouseEvent.MOUSE_OVER,chStopImage);
                
                playSound.addEventListener(MouseEvent.MOUSE_UP,playSong);
                pauseSound.addEventListener(MouseEvent.MOUSE_UP,pauseSong);
                stopSound.addEventListener(MouseEvent.MOUSE_UP,stopSong);
            }
            
            //change button's images
            public function chPlayImage(event:Event):void
            {
                trace('Mouse Over PLAY');
            }
            
            public function chPauseImage(event:Event):void
            {
                trace('Mouse Over PAUSE');
            }
            
            public function chStopImage(event:Event):void
            {
                trace('Mouse Over STOP');
            }
            
            //initialize song function
            public function initSound():void
            {
                _sound=new Sound(new URLRequest(url));
                //_sound=new Sound(new URLRequest(url[n]));
                
                _channel=_sound.play();
                _playing=true;
            }
            //play song function. pag napindot ung play button
            public function playSong(event:MouseEvent):void
            {
                if(_playing)
                {
                    //do nothing since it's already playing
                }
                else
                {
                    //_channel.stop();
                    _channel=_sound.play(_sposition);
                    _playing=true;    
                }
            }
            //pause song function
            public function pauseSong(event:MouseEvent):void
            {
                if(_playing)
                {
                    _sposition=_channel.position;
                    _channel.stop();
                    
                }
                else
                {
                    _channel=_sound.play(_sposition);
                }
                _playing=!_playing;
            }
            //stop song
            public function stopSong(event:MouseEvent):void
            {
                _channel.stop();
                _sposition=0;
                _playing=false;
            }
        }
    }

  3. #3
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    Re: Loading external XML files with Flash CS3

    Hi,
    let's see the XML

  4. #4
    Member Flash Addict gwulfwud is on a distinguished road
    Join Date
    Nov 2007
    Posts
    59
    Rep Power
    3

    Re: Loading external XML files with Flash CS3

    Here you go flep.
    thanks!!

    Code:
    "1.0" encoding="UTF-8"?>
    
    		
    music>

  5. #5
    Member Flash Addict gwulfwud is on a distinguished road
    Join Date
    Nov 2007
    Posts
    59
    Rep Power
    3

    Re: Loading external XML files with Flash CS3

    <***?xml version="1.0" encoding="UTF-8"?>
    <***music>
    <***song title='AmericanJunk.mp3'>
    <***/music>



    just remove the * , as it won't show up without it



  6. #6
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    Re: Loading external XML files with Flash CS3

    The XML and the parse seems ok, paste here the entire error you get please

  7. #7
    Member Flash Addict gwulfwud is on a distinguished road
    Join Date
    Nov 2007
    Posts
    59
    Rep Power
    3

    Re: Loading external XML files with Flash CS3

    1120: Access of undefined property playSound
    1120: Access of undefined property pauseSound
    1120: Access of undefined property stopSound
    and there's another one i just forgot, ill post it after i get home..

    btw, quick question, i notice on your multi audio player that you used another external file for loading the xml, i know it's good practice but will it affect the way my movie clips will behave? coz that error only happened after i added the load xml which i placed inside my main as file..

  8. #8
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    Re: Loading external XML files with Flash CS3

    It does not find playSound, pauseSound and stopSound buttons...

  9. #9
    Member Flash Addict gwulfwud is on a distinguished road
    Join Date
    Nov 2007
    Posts
    59
    Rep Power
    3

    Re: Loading external XML files with Flash CS3

    yeah i know. but before i put in the xml the buttons are working. i don't what i need to add for flash to see those buttons.

  10. #10
    Administrator Living At The FlepStudio! Flep is on a distinguished road
    Join Date
    Jul 2007
    Posts
    5,609
    Rep Power
    9

    Re: Loading external XML files with Flash CS3

    Call initButtons() and initSound() from the end of loadComplete() method


Similar Threads

  1. Loading external swf with no use of AS files
    By Flep in forum Actionscript for beginners - tutorials
    Replies: 34
    Last Post: 28-05-10, 14:12
  2. Problems loading external SWF files
    By scott123 in forum Flash English
    Replies: 0
    Last Post: 15-04-10, 15:40
  3. Loading mulitple external .swf files
    By mrentschler in forum Actionscript 3.0 newbies
    Replies: 2
    Last Post: 06-04-10, 12:54
  4. Replies: 1
    Last Post: 26-12-08, 09:42
  5. Help with loading external flash file
    By srivatson in forum HELP free utilities
    Replies: 0
    Last Post: 08-12-08, 12:32

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts