How to Clear a XML Variable in ActionScript

How to Clear a XML Variable in ActionScript
Page content

What is XML

As you get to know how to clear a XML variable in ActionScript it is best we know what XML is in the first place. XML is an Extensible Markup language that can be used to structure data in a logical manner. XML does this by using HTML like tags that must be well formed. The major difference between XML and HTML is that in XML you can create and use your own tags as you wish while in HTML you are restricted to predefined tags.

XML Variable in ActionScript

An XML variable can be instantiated as an XML object of the XML class. This XML variable is responsible for all the actions done on valid XML data as described above. There is nothing complicated about Instantiating this variable assuming you know your way around Adobe Flash content creation tool. In this case we also assume you are somewhat familiar with ActionScript 3.0 which is relevant to Flash CS3 and above. An example of ActionScript 3 syntax can be found in this tutorial on creating a dropdown menu in Flash.

XML Variable Usage

Using the XML variable is quite easy as you will see. In the example we are going to use, I will simply load data into the XML variable, append more data to it and attempt to clear the XML variable. To set up you scene in Flash you can refer to this tutorial on creating a Flash Dice roller.

The following line creates the instance of the XML variable and loads well formed valid XML data into it. Take note that the XML data must be well formed otherwise the code will fail.

var xml:XML = JaneJohn;

We instantiate a second XML variable this time with information about Jane; our first rider. Take not that the Nodes in the XML are store as a zero indexed or zero based array. This means that you start counting items from 0 and not 1.

var child:XML = xml.rider[0];

Here a third XML variable is created with information about Jane’s bag.

var grandChild:XML = designer;

This line output the contents of the xml variable we initially instantiated


The output of the line above is:


This line outputs Information on Jane.


The output turns out to be:


Next we append information on Jane’s bag and output the information on Jane.

child.appendChild(grandChild );


The output turns out to be:


Clearing the XML Variable

From the steps above you see how easy it is to create and add data into the XML variable. Details on how to clear a XML variable in ActionScript are a bit sketchy. Apparently the ActionScript documentation is not clear on this and the error messages from running ActionScript code do not explain this either. Suppose we want to get rid of Jane’s bag, you would assume doing something like this would work

child.removeChild(grandChild );


delete grandchild

neither work. As a matter of fact removeChild() does not exist and delete only works on dynamically allocated objects. There are three graceful ways to clear content in an XML variable in ActionScript. The first here is use the zero based array notation I mention above. Get the index of the item you want to delete and use that index to delete it. In our case it would be.

delete child.bag[0];

There are cases where you may not know the index. So a safer way is to use the syntax below to get the items parent to trace the element tree and figure out the index of the child node. In this case, the bag. The following line does this

delete grandChild.parent().children()[grandChild.childIndex()];

tracing the output of child after either of these operations shows the node has been cleared. To clear the entire XML variable in ActionScript rather than individual nodes you can set the variable to null using this in our case:

xml = null


Source: author’s own experience.

Screenshots are from author’s own computer