post

Using XML for Flash ActionScript 2 in Articulate, Part 1

Part 1: Understanding XML in Flash

It’s great to build flash objects/assets that can be used, but the problem with most is that they are not reusable. Yes, the code can easily be copied and pasted again and again; however, the use of XML is extremely easy to use/edit and reduces QA time once the look and feel have been created. However, understanding how ActionScript 2 and XML work is not so easy.

We must first understand the basic XML document and its various names/sections. So let’s create and look at a basic XML file.

<?xml version="1.0" encoding="UTF-8" ?>
<data>
	<slide id="1">
		<Filename></Filename>
		<Title><![CDATA[My Title]]></Title>
		<Notes><![CDATA[Nulla vel tempus nibh? Cras congue, nunc quis tincidunt tincidunt; mauris ipsum posuere libero, sit amet bibendum dui nunc et orci? Morbi in tortor turpis, nec auctor eros. Donec volutpat enim nec purus porttitor ullamcorper. Aenean vehicula enim sit amet leo hendrerit consequat nec eget velit! Phasellus mattis vulputate consequat. Morbi suscipit egestas tellus sit amet cursus! Vivamus commodo dui quis augue pellentesque dapibus. Curabitur vel eleifend diam. Praesent et eros purus, et auctor nibh. Ut dapibus egestas orci. Nam sit amet odio nisl.]]></Notes>
		<navlocked>false</navlocked>
	</slide>
</data>

So what you have here is a basic XML document. It has the following elements:

  • XML declaration (<?xml version=”1.0″ encoding=”UTF-8″ ?>)
  • Root Node (<data>)
  • Child Nodes (<Filename>,<Title>,<Notes>,<navlocked>)
  • Attributes (id=”1″)
  • Sibling Nodes (<Filename>’s siblings: <Title>,<Notes>,<navlocked>)
  • Node Values (eg., the node value of the child node <navlocked> is false)

Root Node
The root node is <data>. If we were to eliminate DATA as the root node, then <slide> would become the root node. One neat thing that flash can do is pull the name of the various nodes. To pull the root node name, simply refer to it as myXML.firstChild.nodeName.

Child Nodes
Probably the most important thing to understand about XML documents is the notion of Child Nodes. All XML files must have a root node followed by child nodes. For Flash, it is also important to know that the first child node is called firstChild and the last child node is called lastChild (and note the camel spelling [myTest, myXML], which is when the second word is capitalized when combined with the first word). In flash, child nodes will be called as followsmyXML.childNodes[0] (note that 0 refers to the first one like an array) or myXML.lastChild or myXML.firstChild. Flash also has a way to simply check to see if child nodes exist, e.g., myXML.firstChild.hasChildNodes(). So trace(myXML.firstChild.hasChildNodes()); will output true.

Attributes
In addition, any node can have attributes. In our example above, has an attribute id="1". id is an attribute and must be followed by an equal sign (=) with the value in quotation marks (“”). In flash and using this example, attributes are called myXML.firstChild.childNodes[0].attributes.id. So firstChild calls , then I direct it to its first child node via childNodes[0] (which again is the first one in the array), then to its attribute, specifically id.

Sibling Nodes
Our example also contains sibling nodes, which are nodes at the same level. Note that root nodes cannot have sibling nodes. Flash has the ability to move across siblings skipping over the children (or childNodes). So, myXML.firstChild.childNodes[0].childNodes[0].nextSibling will basically refer to myXML.firstChild.childNodes[0].childNodes[1]. If you were to trace() it would output: <!--[CDATA[My Title]]-->. There is also a previousSibling function that works the same way.

Node Values
Now, probably most important in regards to data obtained from the XML is the node value. The content within each node is called “node values”. So in our example, the node value of the node <navlocked> is false. In flash would be referred to as myXML.firstChild.childNodes[0].childNodes[3].firstChild.nodeValue.

While XMLs can be much more complicated than this, this is a great place to start. The essential flash elements that are used most often are firstChild, lastChild, childNodes[i] (i being a number), attributes, and nodeValue. Knowing how to navigate in an XML file will do wonders for your flash coding, especially as it relates to dealing with Articulate Studio ’09.