Flash XML processing - Process XML data
Home - Tutorials - Actionscript
Flash XML processing is not a complicated task at all. In this article I will show you how to load and process XML files via actionscript.
Tutorial info:
| Name: | Flash XML processing |
| Total steps: | 2 |
| Category: | Actionscript |
| Date: | 2007-11-11 |
| Level: | Beginner |
| Product: | See complete product |
| Viewed: | 27700 |
Bookmark Flash XML processing
Step 2 - Process XML data
Flash XML processing
At this stage we loaded the XML file into our XML object, but how can we work with it? As the load() function returns before the XML file is really loaded we need to stop our movie to wait until everything is loaded and processed.
Code:
var myPhoto:XML; myPhoto = new XML(); myPhoto.ignoreWhite = true; myPhoto.onLoad = processXMLData; myPhoto.load("gallery.xml"); stop();
And how can we recognize if we can continue? It is not so complicated. The XML object has an onLoad event and we can assign a function to this event. Later this function will be called if loading is done. So the processing can begin. Flash will provide us a boolean variable and it tells whether the loading was success or not. With this information we can implement our XML processor function. The skeleton of the function look like this:
Code:
function processXMLData(success) { if (success) { myText.text="Gallery loded"; } else { myText.text="Can not load gallery"; } }
All of the important part will be in the success case. If we have the XML we can read the information from it. We can read the main element, which is the first child in the XML with the following code:
Code:
var myNode; myNode = this.firstChild;
If we want to read out the photo gallery name from the file we need to read the name attributes of the gallery element as follows:
Code:
var galleryName; galleryName = this.firstChild.attributes.name;
To get the number of images in the gallery we need to count all child nodes of the gallery. You can do it as follows:
Code:
var numberOfImages; numberOfImages = this.firstChild.childNodes.length;
Now if you want to load a specific image name and location to display it in your movie you can do it with reading the name and location attributes of the given child node. You can point to any child node in the same way as you select a concrete element from an array.
Code:
var imgName; var imgLocation; imgName = myPhoto.firstChild.childNodes[2].attributes.name; imgLocation = myPhoto.firstChild.childNodes[2].attributes.location;
Previous Step of Flash XML processing
Tags: flash xml processing, actionscript xml processing, flash xml, actionscript xml, flash, xml
| Flash XML processing - Table of contents |
|---|
| Step 1 - XML basics |
| Step 2 - Process XML data |