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]<?xml version="1.0" encoding="UTF-8" ?>
<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>
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)
The root node is <data>. If we were to eliminate DATA as the root node, then
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 (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.
In addition, any node can have attributes. In our example above,
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.attributes.id. So firstChild calls , then I direct it to its first child node via childNodes (which again is the first one in the array), then to its attribute, specifically id.
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.childNodes.nextSibling will basically refer to myXML.firstChild.childNodes.childNodes. If you were to trace() it would output:
<!--[CDATA[My Title]]-->. There is also a previousSibling function that works the same way.
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.childNodes.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.