How to Load Multiple XML Files in Flash

How to Load Multiple XML Files in Flash thumbnail
How to Load Multiple XML Files in Flash

Loading XML into Flash movies is a common task for Flash development. XML holds data in a standardized way and can model a great variety of data types. Many different types of Flash movies and applications use XML data; for example, image galleries, news feeds and slide shows. Load XML into Flash using ActionScript code. To load multiple XML files into Flash, you must come up with an algorithm that will load one at a time, detecting when each one has been loaded, then moving on to the next one.

Instructions

    • 1

      Create your Flash file. In the Flash authoring environment, create a new file and choose a name to save it as. Decide on the visual aspects of your movie such as size and layout. Choose background images or use the drawing tools to create the elements you wish to appear in the movie. Insert a new layer and name it "Actions" to keep your code in one place.

    • 2

      Add code to load XML into your movie. Multiple approaches exist to loading XML content in Flash, mostly dependent on which version of ActionScript you are using. For ActionScript 2.0 you can use the XML Class to process your XML, with a class such as LoadVars being used to actually fetch the XML over a network. For ActionScript 3.0 you can use the URLLoader and URLRequest Classes together with the XML Class to load and process your content. Create an array to hold the URL Strings for each XML file you plan to load. This way, you can use a counter to keep track of which entry is currently being loaded, moving on to the next one when the current file has completed loading.

    • 3

      Add event handlers for your XML loading. Set up an Event Listener for the "COMPLETE" event of your URLLoader object if you're using ActionScript 3.0. For ActionScript 2.0, assign an "onLoad" function for whenever your XML has completed loading. When the Load Event fires, carry out whatever processing you need to handle your XML content, whether this is displaying it or storing it as data somewhere within your ActionScript code.

    • 4

      Add code to move onto the next XML file to be loaded. Once you have carried out whatever functions are necessary to make use of the last chunk of XML content that you loaded, set up code to move onto the next file to be loaded. To do this, you must increment your array counter and load the next URL if there is one. In ActionScript 3.0, create a new URLLoader and URLRequest, using the next XML URL in the array. Reassign the Event Listener by adding it to the new URLLoader instance. In ActionScript 2.0, simply recreate and instruct your loader object, whether this is a LoadVars object or something else, to load the next URL into a new XML object. This makes the same process play out again until your XML files have all been loaded.

    • 5

      Carry out your Flash movie functionality after the XML is loaded. When you complete loading the last XML URL in your array, carry on with whatever the purpose of your Flash movie is. This varies based on what you are creating the Flash file for. If your Flash movie has interactive elements within it such as buttons, and other clickable objects, it's best to disable these while your XML data is being loaded. If you do this, you must enable these elements once the content has completed loading.

Tips & Warnings

  • Include code determining what should happen if the XML fails to load at any point. It is inevitable that this will happen from time to time when you're loading any external content into a Flash file.

  • Don't become frustrated if your XML does not load properly at first. Any iterative function in code normally requires a few attempts before you get it right, so this is perfectly normal.

Related Searches:

References

Resources

  • Photo Credit Jupiterimages/Photos.com/Getty Images

Comments

You May Also Like

Related Ads

Featured