Using XML in Flash CS3/AS3 - Page 3
       by kirupa  |  17 July 2007

Alright, we are making good progress. In the previous page, you learned how to take an external XML file and load it into Flash. Let's look at reading that data in this and subsequent pages.

Reading the XML Data
An important part of dealing with XML data is knowing how to read the data. Before we get to some code related to reading XML data, let's take a look at two classes you'll be using.

XML and XMLList
The first class, the XML class, should already be familiar to you. You declared an XML object earlier, and the data loaded from external file was stored as a new XML object. This class provides you with the basic functionality needed to manipulate and access data stored in an XML file.

The second class that you will use is XMLList. An XMLList is similar to a standard List that stores nothing but XML objects. A major (and cool) difference is that most operations you perform on an XML object can also be applied to an XMLList object.

Accessing Data Directly
In AS3, accessing XML data is more straightforward than it was in AS2. Part of the reason, like I mentioned at the beginning of this article, is the use of E4X. Replace your existing LoadXML function with the following two functions:

function LoadXML(e:Event):void {
xmlData = new XML(e.target.data);
ParseBooks(xmlData);
}
 
function ParseBooks(bookInput:XML):void {
trace("XML Output");
trace("------------------------");
trace(bookInput);
}

Notice that you now have a new ParseBooks function that takes an XML object as an argument. The LoadXML function has also been modified with the trace statement from earlier replaced with a call to the ParseBooks method with our xmlData XML object sent as the argument.

For a sanity check, when you test your application again by pressing Ctrl + Enter, you'll see the same unmodified XML data that you saw earlier:

Example of XML Document

Let's say we wanted to access all of the Book elements from our above data. In AS2, you would have had to write some code to scan our XML file and stop once it had reached the Book node. In AS3, all you have to do is add one extra word to our trace(bookInput) line:

function ParseBooks(bookInput:XML):void {
trace("XML Output");
trace("------------------------");
trace(bookInput.Book);
}

We changed the trace statement from just bookInput to that of bookInput.Book where Book represents the name of element(s) we are interested in. When you run your above code you will only see the nodes that match the Book element name:

That seemed too simple. Let's go a step further. Let's say we only wanted the names of all of the authors located inside our Book nodes. The names of the book authors are stored in the <author> elements, so we change our trace statement from bookInput.Book to bookInput.Book.author:

function ParseBooks(bookInput:XML):void {
trace("XML Output");
trace("------------------------");
trace(bookInput.Book.author);
}

When you test your application again, you will see the following displayed in your Output window:

Notice that you are now seeing a list of author names, but the author names are surrounded by the <author> tags themselves. The reason is you are tracing the actual XML element itself - not the XML element's contents. To retrieve the contents of an XML element, you can use the text() function:

function ParseBooks(bookInput:XML):void {
trace("XML Output");
trace("------------------------");
trace(bookInput.Book.author.text());
}

When you test your application this time, all of the authors are displayed without their surrounding <author> tag names. Unfortunately, the names are all on one line with no space between them:

That is a small side-effect that can easily be changed. One way to fix this is by using index positions to retrieve just the values we are interested in. For example, to retrieve Sir Arthur Conan Doyle, I pass in the index position 0 as in:

function ParseBooks(bookInput:XML):void {
trace("XML Output");
trace("------------------------");
trace(bookInput.Book.author.text()[0]);
}

Running the above code will display Sir Arthur Conan Doyle as we wanted. In many cases, it is probably not convenient to manually enter index values. That is especially true if you do not have advance knowledge of how many nodes or child-nodes your XML data will have. In cases such as that, you will need an indirect, iterative approach different from the direct approach used in this section.

In the next page, let's look at some of these indirect approaches.

1 | 2 | 3 | 4 | 5 | 6 | 7




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.