Use E4X to total the prices of all of the elements with a price attribute : at « XML « Flash / Flex / ActionScript






Use E4X to total the prices of all of the elements with a price attribute

 


package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){

        var cart:XML = <cart>
                          <item price=".98">crayons</item>
                          <item price="3.29">pencils</item>
                          <group>
                            <item price=".48">blue pen</item>
                            <item price=".48">black pen</item>
                          </group>
                        </cart>;
        
        var total:Number = 0;
        
        for each ( var price:XML in cart..@price ) {
          total += price;    
        }
        
        trace( total );

    }
  }
}

        








Related examples in the same category

1.Use E4X to dot down to the particular element node, and then use the @ operator followed by the attribute's name to access its value
2.Use a wildcard (*) with the @ operator to access all attributes of an element
3.To access all of the attributes for a tag, you can use an asterisk
4.@ operator has the attribute() and attributes() methods for accessing attributes by name or as an XML list respectively.
5.Use bracket to access an attribute by name, or when the attribute name contains characters not allowed in variable names
6.Reference third level tag value
7.Deleting Elements and Attributes: To remove an element or attribute from a document, use the delete operator as follows: delete elementOrAttribute