WAYS TO USE E4X TO FILTER DATA IN ACTIONSCRIPT 3

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:

你可能感兴趣的:(.net,xml,Flash,Access,actionscript)