XML-Driven Drop-Down Menu
         by senocular

Beginning the Coding Process
We have our XML format mapped out. Now its just a matter of interpreting the XML so that Flash will know how to construct a menu based on information provided. This is often the more difficult part in dealing with XML. It's not too hard to write the XML or even look at it with your own eyes to tell what's going on in there. But once you start to try to extract its contents using some programming language like Actionscript, you can start to get confused. If you're brand new to using XML in Flash, you may first want to read some other beginner XML tutorials here on the site to help you get acquainted. Otherwise, we'll just jump right on in.

Loading and Interpreting XML
Loading an XML file is easy. The concept of having to wait for it to load can be difficult. Often people want to use it directly after the load command which is just not possible. Being an external file, it will take some time, frames even, to completely load into the Flash player. Only then can it be used. Understanding that, we know enough to not have any actions aside from the loading of the XML until the onLoad event called in receiving file. Because of this, and because of portability and reuse of code, everything done will be based around a series of functions that will be called when needed.

Once loaded, there will be 2 basic types of information needed from the XML. This includes the node name for each node and values of the attributes of that node (i.e. name, action and variables). Whenever a particular menu is created, whether it be the main menu or a submenu these values will need to be extracted and used in the creation of a menuitem movieclip. Before doing so, however, we'll need to get to all the XML nodes, or at least those associated with whatever menu we're dealing with at the time. The best way of doing this is to simply loop through all the childNodes of a particular node in the XML object. This of course will start at the first node in the XML which is the main menu. This is represented as


Child nodes are given through that (or any) node's childNodes array. This is what is looped through in order to reach all menu items for any menu node.


For name of a node, we can use the nodeName property.


Attributes of a node are extracted from an attributes object contained within the Flash node. The values of the attributes can then be retrieved by specifying the attribute by its name within the attributes object.


With that, here's an Actionscript example looping through and showing the needed values of all the contents, or items, of the main menu (the first menu node) as specified in the XML file.

xmlNode = xmlObject.firstChild;
for (var i = 0; i < xmlNode.childNodes.length; i++){
// either "menu" or "item"
// name of the item
// action of the item
// variables for that action

Not too hard right? This is probably the hardest part, just extracting the needed info from the XML file. With this, its time to get Flash to put it to work. The XML information is just text at this point. Further Actionscript will be needed to make it all function.





kirupa.com's fast and reliable hosting provided by Media Temple.