Flash / Flex / ActionScript/XML/Bracket syntax — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 09:19, 26 мая 2010

Accessing Values with E4X: Using the Dot Operator to Access Elements

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var movieList:XML = <movieList>
                                <listName>My favorite movies</listName>
                                <movie id="123">
                                    <title>Titus</title>
                                    <year>1999</year>
                                    <director>J T</director>
                                </movie>
                                <movie id="456">
                                    <title>Rushmore</title>
                                    <year>1998</year>
                                    <director>W A</director>
                                </movie>
                                <movie id="789">
                                    <title>Hall</title>
                                    <year>1977</year>
                                    <director>Woody Allen</director>
                                </movie>
                           </movieList>;
        
        trace(movieList.listName); // Displays : My favorite movies
        
        trace(movieList.movie[1]);
        
        trace(movieList.movie[0].title); 
        trace(movieList.movie[2].director);
    }
  }
}



Adding Text Nodes to an XML Object

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:XML = <example/>;
        
        example.firstname = "Darron";
        // Create a text node from a number
        example.number = 24.9;
        // Create a text node from a boolean
        example.boolean = true;
        
        // Create a text node from an array
        example.abc = ["a", undefined, "b", "c", null, 7, false];
        
        trace( example );
    }
  }
}



Bracket syntax is required for illegal property name

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:XML = <example />;
        
        example[ "some-element" ] = "";
        
        trace( example );
    }
  }
}



Build a property name and, consequently, an element name, dynamically

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:XML = <example />;
        
        var id:int = 10;
        
        // Create a string to incorporate the value of id in the node name
        example[ "user" + id ] = "";    
        
        trace( example );
    }
  }
}



Use a variable"s value as the element name to find:

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var nodeName:String = "color";
        var fruit:XML = <fruit><color>red</color></fruit>;
        trace( fruit[nodeName] );
    }
  }
}



Use bracket notation, coupled with an integer, to access a particular element node

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var items:XML = <items>
                          <item>
                            <name>Apple</name>
                            <color>red</color>
                          </item>
                          <item>
                            <name>Orange</name>
                            <color>orange</color>
                          </item>
                        </items>;
                        
        
        trace( items.item[0].name );
        
        trace( items.item[1].name );
    }
  }
}



XML text in a String into an actual XML object, whereas a cast would fail.

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var myXml:XML = XML("<root><party><time>Now</time><location>Here</location></party></root>");
        trace(myXml.party[0].time); //Now
    }
  }
}