Reading Elements in an XML Tree : XML Object « XML « Flash / Flex / ActionScript






Reading Elements in an XML Tree

 

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


        var menu:XML = <menu>
                         <menuitem label="File">
                           <menuitem label="New"/>
                         </menuitem>
                         <menuitem label="Help">
                           <menuitem label="About"/>
                         </menuitem>
                         This is a text node
                       </menu>;
        
        for each ( var element:XML in menu.elements(  ) ) {
          trace( element.@label );
        }
        var menu:XML = <menu>
                         <menuitem label="File">
                           <menuitem label="New"/>
                         </menuitem>
                         <menuitem label="Help">
                           <menuitem label="About"/>
                         </menuitem>
                         This is a text node
                       </menu>;
        
        walk( menu );
        
        function walk( node:XML ):void {
          for each ( var element:XML in node.elements(  ) ) {
            trace( element.@label );
            walk( element );
          }
        }
    }
  }
}

        








Related examples in the same category

1.Creating an XML Object
2.Create the XML structure with a string
3.Understanding the XML Classes
4.Using Expressions within Literals: embed ActionScript code directly into your XML data.
5.Adding Attributes to an XML Element
6.Finding Elements by Name
7.Reading Text Nodes and Their Values