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. We can do that too, but not directly through E4X (as far as I know please prove me wrong!).
Say the application's interface allows the user to filter across multiple fields, but each field is optional. Var items:XMLList = em.(attribute(filterBy) = filterValue) įinally, let's finish with something a little complex. This will also work for the child function. Here, we call the attribute() to get an attribute's value by its name. 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. Let's make things a little more interesting. In exact terms, (source = source) most definitely won't work! 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. Please note that you must be sure the variable name is different than the name of the child. Var itemNames:XMLList = em.(source = sourceName) E4X will let you compare attributes or children against a variable as well! However, the filtering is controlled by the user through a ComboBox or another user interface component. Say that your application filters items by source, like in the example where we only wanted items from Amazon. Var items:XMLList = em.(source = "Amazon" & price <400) This example filters items from Amazon with a price under $400.00. SWITCH TO PLAIN can filter by multiple fields as well. I find this particularly useful for debugging. In the following example, I've included a trace statement to display each item's name in the console. Interestingly enough, you can place just about any ActionScript statement within the parentheses. Var amazonItems:XMLList = em.(source = "Amazon") In this case, we check to see if an item's source is equal to a string value. Place a statement within the parentheses to check for a specific value. To take it a step further, you can filter the same set of data to find items with "Amazon" defined as the source value. Notice that the name attribute is specified with the symbol at the beginning.
The following example gets a list of all the item names defined in the XML from the previous example. It's supported natively by the compiler.Į4X 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. However, many developers might miss the fact that you can declare XML directly within your classes or ActionScript code. The most common way to declare a variable of one of these types is to pass it and XML string. Read on for a super quick introduction to E4X followed by some powerful ways to filter your data using this feature.Į4X is powered by the new XML and XMLList classes. It's called E4X, and it gives us developers some useful new syntax. ActionScript 3 in Flash 9 includes powerful support for reading and manipulating XML.