written by: S. R. Obbayi•edited by: Amy Carson•updated: 2/25/2011
Before you begin to know how to clear a XML variable in ActionScript it is good to understand that XML content can be wide and varied and this can be intimidating to the new users of ActionScript. This cannot be further from the truth. This article explains it all.
slide 1 of 5
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.
slide 2 of 5
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.
slide 3 of 5
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 = <bus><rider id="1">Jane</rider><rider id="2">John</rider></bus>;
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;
Here a third XML variable is created with information about Jane's bag.
var grandChild:XML = <bag>designer</bag>;
This line output the contents of the xml variable we initially instantiated
Next we append information on Jane's bag and output the information on Jane.
The output turns out to be:
slide 4 of 5
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
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.
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
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: