Using XML data in Flash : Flash and XML
Nearly everything you will do with XML in Flash involves the use of the XML class and involves one of the following:
-
Formatting XML. Create an XML document in Flash.
-
Parsing XML which is a fancy term for accessing the XML nodes.
-
Loading XML into Flash from an XML document.
-
Sending XML to a URL.
Though this lesson only deals with parsing and loading, I will cover off how each of the four tasks is accomplished
Flash 1: The XML player.
Creating a new XML object in Flash
The XML class in Flash has several methods you can utilize to create and format an XML document. There are 15 methods ranging from XML.addRequestHeader() to XML.toString() and, if you are new to Flash and XML you will quickly discover how difficult they are to use. There is a better way, according to Derek Franklin, author of "Flash MX 2004 actionscript Training from The Source". He suggests creating a string and converting it to an XML object which is a much easier and more common approach to the creation of XML objects.
An XML object in Flash is created using the following code:
var flashXML:XML = new XML();
In object-oriented programming a class defines a category of an object. A class describes the properties (data) and the behavior (method) for an object. To use a class' methods and properties you must first create an instance of the class. That is the purpose of the word "new". It creates a new instance of the class. That instance is referred to as an "object". We will be creating sound and video objects using this syntax later on in the course.
To hand the new XML object the XML-formatted data when it is created, you can put the name of the XML data between the empty brackets. You can also use the parseXML() method to parse an XML document/string into an existing xml object
Let's assume you want to create the following XML document in Flash:
<MyTeachers>
<Name Title= "Coordinator">James Cullin</name>
<Name Title= "Instructor">Tom Green </name>
</MyTeachers>
To accomplish this you would first create the document as a string and then convert that string into an XML object by placing it in new XML(). Here's the code that accomplishes that task:
var myString: String = <MyTeachers>
<Name Title= \ "Coordinator\">James Cullin</name>
<Name Title=\ "Instructor\">Tom Green </name> </MyTeachers>;
var myXML: XML = new(myTeachers);
All the code does is to create the string and convert it to an XML object called "myXML". This object can then be sent to a server using the send() method which I'll explain later. This example is terribly inefficient. This is for illustration purposes only. A cleaner way would be to simply send the string in the first place.
Accessing XML data
Do you remember your high school or grade school English classes where you had to identify the parts of a sentence such as subject, predicate, conjunction and phrase? What you were doing was "parsing" a sentence by breaking it down into its parts. When someone tells you they are writing a script that parses an XML document all they are really saying is the script will extract information from the XML document and somehow use it in Flash.
When you parse an XML document you are converting it from a string- a collection of words- into a logical grouping of nodes. Once the XML doucment has been parsed you can then access it's various elements using the XML node properties that Flash generates during the parsing process (i.e. nextSibling, nodeValue, etc). In many respects think of the parsing process as "skipping" along the parsed XML object to access or manipulate the data in the nodes.
When you call the XML load() method it automatically downloads the XML file you specify, and when the download is completed it automatically parses it for you.
There are 18 properties used by the XML and rather than get into each one, I am going to deal with a few of the most common using the XML document from the previous section. They are:
firstChild: This property points to the first node in the tree structure. For example:
myXML.firstChild.firstChild
would return:
<name Title= "Coordinator">James Cullin</name>
The first child node of the XML document is the root node <MyTeachers> and the first child of that node is "name".
childNodes: This property returns a list (Array) of the child nodes at any point in the XML document. An example would be:
var myArray: Array = myXML.firstChild.childNodes.
nextSibling: This property points to the next node in the same level of the XML tree structure. For example:
myXML.firstChild.nextSibling
would return
<name Title= "Instructor">Tom Green </name>
attributes: This property hands off an associuative array or attribute names. For example:
myXML.firstChild.firstChild.nextSibling.attributes. Title
would return:
instructor
Loading XML data in Flash
When you first start working with XML data in Flash you will mostly work with it when you are loading or sending out the XML. To load XML from a remote source such as your site:
- Create the XML object
- Load the XML ,using the load() method, into the .swf.
The code to do this would be:
var myXML:XML = new XML();
myXML. load("/teachers.xml");
Sending XML data out of Flash
As I pointed out earlier you can create your own XML document right in Flash. The really interesting thing about the XML class is that you send and load the document "almost" simultaneously.
I use the word 'almost" because technically Flash will never simulataneously send and load an XML document.If you think about that you can grasp this concept. The time between Flash starting to send the XML document and loading the server's response will take time. The amount of time is totally dependent upon the amount of XML data that's being sent/recieved.Add to that time your connection speed/lag to the server -and back- as well as the amount of time it takes the server to recieve the data, parse it, process it, reformat it's own XML response and transmit that back and , as you see, there will be a delay. In most cases all of this can happen in the course of a second or two... some cases it can take several seconds.
Sending the data uses the send() method and a destination URL. It would look like this:
var myXML:XML= new XML(<Greeting><Text>Hello World!</text></Greeting>
myXML.send ("/apage.asp");
In the next section we build a Flash file that access the data in an XML document and uses it to construct a Picture Viewer.
![]()
