`
nlslzf
  • 浏览: 1045474 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

WAYS TO USE E4X TO FILTER DATA IN ACTIONSCRIPT 3

    博客分类:
  • flex
阅读更多
http://joshblog.net/2007/05/08/methods-to-filter-data-with-e4x-in-flash-9/

ActionScript 3 in Flash 9 includes powerful support for reading and manipulating XML. It's called E4X, and it gives us developers some useful new syntax. Read on for a super quick introduction to E4X followed by some powerful ways to filter your data using this feature.

E4X is powered by the new XML and XMLList classes. The most common way to declare a variable of one of these types is to pass it and XML string. However, many developers might miss the fact that you can declare XML directly within your classes or ActionScript code. It's supported natively by the compiler.

SWITCH TO PLAIN TEXT
Actionscript:
var data:XML =
    <items>
        <item name="Wii">
            <source>Amazon</source>
            <price>364.00</price>
        </item>
        <item name="Wii">
            <source>Target</source>
            <price>249.99</price>
        </item>
        <item name="X-Box 360">
            <source>Amazon</source>
            <price>399.99</price>
        </item>
        <item name="PlayStation 3">
            <source>Amazon</source>
            <price>599.99</price>
        </item>
    </items>;

E4X lets you drill down into the data to get the information you need without any excessive looping or strange calls to parent and child nodes. The following example gets a list of all the item names defined in the XML from the previous example. Notice that the name attribute is specified with the "@" symbol at the beginning.

SWITCH TO PLAIN TEXT
Actionscript:
var itemNames:XMLList = data.item.@name;

To take it a step further, you can filter the same set of data to find items with "Amazon" defined as the source value. Place a statement within the parentheses to check for a specific value. In this case, we check to see if an item's source is equal to a string value.

SWITCH TO PLAIN TEXT
Actionscript:
var amazonItems:XMLList = data.item.(source == "Amazon");

Interestingly enough, you can place just about any ActionScript statement within the parentheses. In the following example, I've included a trace statement to display each item's name in the console. I find this particularly useful for debugging.

SWITCH TO PLAIN TEXT
Actionscript:
data.item.(trace(@name));

You can filter by multiple fields as well. This example filters items from Amazon with a price under $400.00.

SWITCH TO PLAIN TEXT
Actionscript:
var items:XMLList = data.item.(source == "Amazon" && price <400);

Let's take it a step further. Say that your application filters items by source, like in the example where we only wanted items from Amazon. However, the filtering is controlled by the user through a ComboBox or another user interface component. E4X will let you compare attributes or children against a variable as well!

SWITCH TO PLAIN TEXT
Actionscript:
var sourceName:String = "Target";
var itemNames:XMLList = data.item.(source == sourceName);

Please note that you must be sure the variable name is different than the name of the child. If I had named my variable source, instead of sourceName, the statement within the parentheses would never access the item's source value because it would always use the variable. In exact terms, (source == source) most definitely won't work!

Let's make things a little more interesting. What happens if the name of the value by which we're filtering should be dynamic as well? In the previous examples, we've accessed attributes directly, but you can call functions on the XML object too. Here, we call the attribute() to get an attribute's value by its name. This will also work for the child function.

SWITCH TO PLAIN TEXT
Actionscript:
var filterBy:String = "name";
var filterValue:String = "Wii";
var items:XMLList = data.item.(attribute(filterBy) == filterValue);

Finally, let's finish with something a little complex. Say the application's interface allows the user to filter across multiple fields, but each field is optional. We can do that too, but not directly through E4X (as far as I know; please prove me wrong!). This example shows how to filter by items from Amazon that cost exactly $599.99, but the fields Array could contain any number of items with additional items to filter by.

SWITCH TO PLAIN TEXT
Actionscript:
var filtered:XMLList = data.item;
var fields:Array = [{name: "source", value: "Amazon"}, {name: "price", value: "599.99"}];
       
var fieldCount:int = fields.length;
for(var i:int = 0; i <fieldCount; i++)
{
    var fieldName:String = fields[i].name;
    var fieldValue:String = fields[i].value;
    filtered = filtered.(child(fieldName) == fieldValue);
}

I'm sure you can think of interesting ways to expand on that last example to create some very powerful filtering systems. In this particular case, I've only checked for equality, but with some simple changes to the logic, you could check for prices greater than or less than a certain value, or you could even search for substrings within a value. For instance, you might want to search for "PlayStation", and include results for PS2 and PS3 systems. E4X is very, very powerful.

Update: I've written two followup articles:
分享到:
评论

相关推荐

    Foundation ActionScript 3.0 with Flash CS3 and Flex

    To really harness the power of Flash though, you need to make use of ActionScript to provide dynamic effects, enable user interaction, and manipulate data. ActionScript 3.0, the latest version of ...

    The Essential Guide to Flex 2 with ActionScript 3.0 源代码

    After learning how to install and becoming familiar with the basics of the Flex Builder 2 software, you will explore in depth how ActionScript 3.0 interacts with Flexs powerful XML-like design ...

    加载loader (How to Load External Images in Actionscript 3.0)

    在ActionScript 3.0中,加载外部资源是常见的需求,特别是加载图片,这对于创建动态内容、交互式应用或者富媒体展示来说至关重要。本篇文章将深入探讨如何在ActionScript 3.0中加载外部图像,即Loader类的使用。 1....

    actionscript3

    本书是国内第一本“面向原因式”(Why-Oriented Book)、全面系统介绍Flash ActionScript 3的书籍。全书共分为5个部分。第一部分:ActionScript 3语言基础;第二部分:ActionScript 3 面向对象编程;第三部分:...

    The Essential Guide to Flex 2 with ActionScript 3.0-part 1

    After learning how to install and becoming familiar with the basics of the Flex Builder 2 software, you will explore in depth how ActionScript 3.0 interacts with Flexs powerful XML-like design ...

    The Essential Guide to Flex 2 with ActionScript 3.0-part 2

    After learning how to install and becoming familiar with the basics of the Flex Builder 2 software, you will explore in depth how ActionScript 3.0 interacts with Flexs powerful XML-like design ...

    ActionScript3中文手册

    ActionScript3是Adobe Flash平台的核心编程语言,用于创建交互式内容、富互联网应用程序(RIA)以及游戏。这个“ActionScript3中文手册”是为开发者提供的一份详细文档,旨在帮助他们理解和掌握ActionScript3的基本...

    actionscript 3 as3 pdf

    as3 pdf下载 ActionScript 3.0 是一种强大的面向对象编程语言,它标志着 Flash Player Runtime 演化过程中的一个重要阶段。设计 ActionScript 3.0 的意图是创建一种适合快速地构建效果丰富的互联网应用程序的语言,...

    Learning ActionScript2.0 in Flash

    《深入学习ActionScript 2.0在Flash中的应用》 一、ActionScript 2.0概览 ActionScript 2.0是Adobe Flash平台的一种强大脚本语言,它为动画和交互式媒体提供了丰富的功能。在Flash软件中,ActionScript 2.0使...

    Foundation XML and E4X

    AS3标签则表明内容将重点讨论ActionScript 3与XML的结合,包括E4X的使用。 **知识点概览:** 1. **XML基础**:书籍可能会介绍XML的基本语法,如元素、属性、命名空间、文档类型定义(DTD)和XML Schema。 2. **...

    Design Patterns in ActionScript

    《ActionScript设计模式》是软件开发领域中针对ActionScript编程语言的一种实践指南,它深入探讨了如何在ActionScript项目中应用经典的设计模式。设计模式是软件工程中的宝贵经验总结,它们是解决常见问题的可复用...

    Flash ActionScript 3殿堂之路

    《Flash ActionScript3殿堂之路》共分5个部分。第1部分:ActionScript 3语言基础;第2部分:ActionScript 3面向对象编程;第3部分:ActionScript 3 核心类;第4部分:ActionScript 3主要的Flash Player API;第5部分...

    ActionScript 3 类型转换

    在深入探讨ActionScript 3类型转换的精妙之前,我们先来回顾一下ActionScript脚本语言,这是一种广泛应用于Adobe Flash平台的编程语言,主要用于创建动态交互式内容、游戏以及动画等。随着版本的演进,ActionScript...

    actionscript

    ActionScript 3.0 是一种基于ECMAScript for XML (E4X) 的脚本语言,主要用于Adobe Flash Platform,包括Flash Player和Adobe AIR。它在2006年随着Flash Player 9一同发布,带来了显著的性能提升、类型检查以及面向...

    Popup window ActionScript3 VS ActionScript2

    ```actionscript3 import flash.display.MovieClip; import flash.display.Stage; var popup:MovieClip = new PopupMC(); popup.x = stage.stageWidth / 2; popup.y = stage.stageHeight / 2; stage.addChild(popup...

    Foundation XML and E4X for Flash and Flex

    - **E4X表达式**:E4X使得ActionScript可以直接操作XML数据,如同操作普通的JavaScript对象一样。通过简单的表达式就可以实现对XML数据的读取、修改等操作。 #### 三、在Flash和Flex中利用XML构建动态Web应用 ...

    《ActionScript 3.0 语言和组件参考》中文官方版本,无错

    ActionScript 是针对 Adobe Flash Player 运行时环境的编程语言,它在 Flash 内容和应用 程序中实现了交互性、数据处理以及其它许多功能。 ActionScript 是由 Flash Player 中的 ActionScript 虚拟机 (AVM) 来执行的...

    Flex4 Declarations in ActionScript

    《Flex4声明在ActionScript中的应用》 Flex4框架是Adobe Flex的一个重要版本,它引入了许多新特性,尤其是在ActionScript编程中的声明式编程方式。这一技术显著提升了开发效率,使得开发者能够更直观地定义UI组件和...

Global site tag (gtag.js) - Google Analytics