Previous Page
Next Page

Recipe 20.7. Finding Elements by Name

Problem

You want to find an element by its node name rather than by its position in the XML hierarchy.

Solution

Use E4X syntax to "dot down" to a particular element.

Discussion

E4X makes working with XML objects as simple as working with regular objects. For each element node, you access that element in the same way you access the property of an object; for example:

var fruit:XML = <fruit><name>Apple</name></fruit>;

// Displays: Apple
trace( fruit.name );

As you can see here, to access the name element in the XML packet, use the dot operator (.) and reference name directly of the fruit XML instance. For elements nested deeper in the XML tree, use a series of dots and element names to reach the element, as follows:

var author:XML = <author><name><firstName>Darron</firstName></name></author>;

// Displays: Darron
trace( author.name.firstName );

One shortcut to consider for accessing deeply nested element nodes without knowing the path to get to the node is to use the double-dot operator. For example, you can omit the path and extract the firstName node directly, like this:

var author:XML = <author><name><firstName>Darron</firstName></name></author>;

// Displays: Darron
trace( author..firstName );

The double-dot operator works for any level of nesting; it doesn't matter if the target node is two or ten levels deep.

When there are multiple element nodes that share the same name, accessing the element name from an XML instance becomes indexable, much like an Array. Use bracket notation, coupled with an integer, to access a particular element node; for example:

var items:XML = <items>
                  <item>
                    <name>Apple</name>
                    <color>red</color>
                  </item>
                  <item>
                    <name>Orange</name>
                    <color>orange</color>
                  </item>
                </items>;
                
// Displays: Apple
trace( items.item[0].name );
// Displays: Orange
trace( items.item[1].name );

Using items.item returns an XMLList with two elements, each representing one of the item element nodes in the XML literal. The first item element node can be accessed by examining index 0, and the second item element node is accessed with index 1. The first element index is always 0, and the last element index is always one less than the total number of elements. Use the length( ) method to count the number of elements found:

// Displays: 2
trace( items.item.length(  ) );

If you want to examine every element node with a particular node name and you don't know how many there are, use a for each loop, like this:

var items:XML = <items>
                  <item>
                    <name>Apple</name>
                    <color>red</color>
                  </item>
                  <item>
                    <name>Orange</name>
                    <color>orange</color>
                  </item>
                </items>;

// Use the double dot syntax so we can omit the path
for each ( var name:XML in items..name ) {
  /* Displays:
  Apple
  Orange
  */
  trace( name );    
}

As with other applications of the dot operator, you can use bracket notation instead of the dot operator. This is useful for situations in which you want to use a variable's value as the element name to find:

var nodeName:String = "color";

var fruit:XML = <fruit><color>red</color></fruit>;

// Displays: red
trace( fruit[nodeName] );

Bracket notation cannot be used with the double-dot operator. For example, the following code generates an error:

trace( fruit..[nodeName] ); // Generates a compile error

See Also

Recipe 20.14


Previous Page
Next Page
Converted from CHM to HTML with chm2web Pro 2.85 (unicode)