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

In the previous page, you learned several ways of accessing the attributes located in your XML file. In this page, we look at something familiar to those who wrote SQL or related statements when querying databases - the filtering mechanism in E4X.

Filtering Values
Another new feature in AS3 is the ability to filter and display only the data you are interested in. In many examples in the preceding pages, you scanned through XML objects and tried to see if a name matched the value you are looking for. There exists a better way!

Filtering Node Values
Let's say I wanted to find all books by Stephen E. Ambrose from my XML data. I could create a function that scans each XML object's value and returns the parent XML node once the author value equals Stephen E. Ambrose. In AS3, a simpler, more powerful approach exists, and the following is my code for returning all books whose author was Stephen E. Ambrose:

function ParseBooks(bookInput:XML):void {
trace("XML Output");
trace("------------------------");
 
var authorList:XMLList = bookInput.Book.(author == "Stephen E. Ambrose");
trace(authorList);
}

When you run the above code, your Output window will display the following:

Notice that the data returned are the actual XML objects that contained XML nodes named author whose value was Stephen E. Ambrose. This was possible because of the filtering mechanism introduced in AS3:

var authorList:XMLList = bookInput.Book.(author == "Stephen E. Ambrose");

Instead of specifying bookInput.Book.author, returning a list of XML objects, and scanning each object for the author name Stephen E. Ambrose, I simply provide the keyword, a comparison operator, and the value that I want to search for. The rest are taken care of behind the scenes, and what you are left with is a collection of XML objects that match the criteria you specified.

While you found the results you were looking for, your results came back in the form of XML objects. To display the actual titles of books, sans XML information, written by Stephen E. Ambrose, you can simply append the .title keyword following your filter command:

var authorList:XMLList = bookInput.Book.(author == "Stephen E. Ambrose").title;

The reason you can do this is because the results of your filter command are also returned in the form of an XMLList. Any operations you perform on an XMLList, such as searching for the title, are applied to each XML object stored within the XMLList also.

Filtering Attribute Information
Filtering your data based on attribute information is only slightly different. To return a list of books whose ISBN matches a certain value, all you need to do is prepend the @ symbol in front of your keyword.

Try out the following code:

function ParseBooks(bookInput:XML):void {
trace("XML Output");
trace("------------------------");
 
var bookList:XMLList = bookInput.Book.(@ISBN == "0743203178").title;
trace(bookList);
}

When you run your application with the above ParseBooks function, you will see Nothing Like It In the World displayed. Let's look at the authorList declaration in greater detail:

var authorList:XMLList = bookInput.Book.(@ISBN == "0743203178").title;
trace(authorList);

Instead of typing in ISBN == "....", you have to prepend the keyword with the @ symbol to flag that keyword as an attribute keyword. By typing @ISBN == "0743203178", any nodes whose ISBN attribute matches that number is returned.


Onwards to the next page!

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




SUPPORTERS:

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