Filtering XML Data Using AS3
Filtering data XML data using AS2 has to be one of the most sought out features when dealing with XML-driven Flash projects. Being able to automatically grab certain relevant information without manually searching each and every XML node and/or attribute.
What if we were given the ability to create filters that will automatically pull the information needed from XML nodes and/or attributes. But wait we can’t do that, right? Right, that is until the birth of ActionScript 3.0.
Another new feature in AS3 is the ability to filter and display only the data you are interested in. With this, you can dynamically filter XML nodes and attributes.
Filtering XML Node Values
Let’s say you wanted to pull all the of the books that were paperback. You can now create a function that scans each XML’s node or attribute value and returns which node would qualify with the given query. See the example below in which outputs the books in which are paperback.
Note: I am using this XML file in the example.
var xmlLoader:URLLoader=new URLLoader ;
var xmlData:XML=new XML ;
//Adding an event listener to notify when loading is completed
xmlLoader.addEventListener(Event.COMPLETE,LoadXML);
//Load the XML file
xmlLoader.load(new URLRequest("book-list.xml"));
function LoadXML(e:Event):void {
xmlData=new XML(e.target.data);
ParseBooks(xmlData);
}
function ParseBooks(bookInput:XML):void {
//Creating an xml list that will store the book info
var books:XMLList=bookInput.book.children();
//Creating a xml list that will store the book's ISBN number
var bookAttributes:XMLList=bookInput.book.attributes();
//Filter out all of the books with a "Paperback" cover
var paperBackList:XMLList=bookInput.book.coverType=="Paperback";
trace(paperBackList);
}
Filtering XML Attributes
Not only can you filter XML node values but you can do the same to XML Attributes values. In the a previous post , Loading XML Data, I created and included an attribute labeled “ISBN” which would be available within each book node. If you wanted to return a list of books that matched a certain value, it would go something like this.
Note: I am simply replacing the ParseBooks function from the code above
function ParseBooks(bookInput:XML):void {
//Creating an xml list that will store the book info
var books:XMLList=bookInput.book.children();
//Creating a xml list that will store the book's ISBN number
var bookAttributes:XMLList=bookInput.book.attributes();
//Filter out the book with a matching ISBN number
var closeMatchList:XMLList=bookInput.book.@isbn=="978-0596526948";
trace(closeMatchList);
}
As you can see, there isn’t much of a difference with filtering by XML node values or attribute information. Filtering can provide such a dynamic form of functionality to any Flash project: big or small.
Download Source
I’d love to hear from you. What do you think of this new functionality when dealing with XML data?
Feedback:
I hope this helps in your migration over into AS3. If there is something in particular you want to add or see with the Moving From AS2 to AS3 series, feel free to contact me via the contact page.
Subscribe to the RSS Feed to stay updated on future tutorials on using XML in AS3.











7 Comments
Dan
03.05.2009
Excellent post
Cashmere
03.05.2009
Very helpful. Thanks for sharing.
Angel
03.05.2009
No problem, Cashmere!
colep
03.27.2009
dope!
Justin
06.09.2009
Thanks! This has revolutionized my life.
Bruno Amorim
10.22.2009
Thank’s a lot men… Great script.
Emile
05.04.2011
Awesome! Thanks a lot, it’s a big help!
There are no trackbacks to display at this time.