Flash / Flex / ActionScript/XML/XML Object

Материал из Web эксперт
Версия от 08:14, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Adding Attributes to an XML Element

 

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

        var example:XML = <example><someElement/></example>;
        
        example.someElement.@number = 12.1;
        example.someElement.@string = "example";
        example.someElement.@boolean = true;
        example.someElement.@array = ["a", null, 7, undefined, "c"];
        
        trace( example );
        
        example.someElement.@["bad-variable-name"] = "yes";
        
        example.someElement.@["color" + num] = "red";
    }
  }
}



Create the XML structure with a string

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var str:String = "<gamescore><username>username </username>"
                       + "<score>111</score></gamescore>";
        
        // Pass the string to the constructor to create an XML object
        var example:XML = new XML( str );
    }
  }
}



Creating an XML Object

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:XML = <abc><a>eh</a><b>bee</b><c>see</c></abc>;
        var username:String = "Darron";
        var score:int = 1000;
        
        var example:XML = <gamescore>
                             <username>{username}</username>
                             <score>{score}</score>
                          </gamescore>;
    }
  }
}



Finding Elements by Name

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var fruit:XML = <fruit><name>Apple</name></fruit>;
        
        trace( fruit.name );
        
        var author:XML = <author><name><firstName>Darron</firstName></name></author>;
        
        trace( author.name.firstName );
    }
  }
}



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 );
          }
        }
    }
  }
}



Reading Text Nodes and Their Values

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var book:XML = <book>
                         <title>ActionScript</title>
                       </book>;
        
        var title:String = book.title.toString(  );
        
        trace( title );
    }
  }
}



Understanding the XML Classes

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var myXML:XML = <message>Hello, world!</message>;
//        An XMLList is a numbered set of XML objects.
        var list:XML = <list>
                           <item id="58" />
                           <item id="135" />
                           <item id="12" />
                       </list>;
        var items:XMLList = list.item;
        trace(items);
        trace(items.length());
        trace(items[1].toXMLString());
    }
  }
}



Using Expressions within Literals: embed ActionScript code directly into your XML data.

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var squareLength:Number = 10;
        var circleRadius:Number = 5;
        var shapeData:XML = <shapeList>
                               <shape type="square" size={squareLength} />
                               <shape type="cicle" size={circleRadius} />
                            </shapeList>;
        trace(shapeData.toXMLString());
    }
  }
}