Flash / Flex / ActionScript/XML/at

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

Deleting Elements and Attributes: To remove an element or attribute from a document, use the delete operator as follows: delete elementOrAttribute

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

        var novel:XML = <BOOK ISBN="0000000000">
            <TITLE>ActionScript</TITLE>
            <AUTHOR>J, J</AUTHOR>
            <PUBLISHER>Books Ltd</PUBLISHER>
          </BOOK>;
        
        trace(novel);
        delete novel.@ISBN;
        trace(novel);
        
    }
  }
}



@ operator has the attribute() and attributes() methods for accessing attributes by name or as an XML list respectively.

 
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>;
        
        movieList.movie[1].attribute("id"); // 456
        movieList.movie[2].attributes(); // 789
        //The preceding code is functionally identical to the following code:
        movieList.movie[1].@id; // 456
        movieList.movie[2].@*; // 789

    }
  }
}



Reference third level tag value

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var fruits:XML = <fruits>
                           <fruit color="red">
                             <name>Apple</name>
                           </fruit>
                           <fruit color="orange">
                             <name>Orange</name>
                           </fruit>
                           <fruit color="green">
                             <name>Pear</name>
                           </fruit>
                           <fruit color="red">
                             <name>Watermelon</name>
                           </fruit>
                         </fruits>;
                         
        trace( fruits.fruit.(name == "Apple").@color );

    }
  }
}



To access all of the attributes for a tag, you can use an asterisk

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var myXML:XML = <foo a="1" b="2" c="3" />;
        trace(myXML.@*.toXMLString()); 
    }
  }
}



Use a wildcard (*) with the @ operator to access all attributes of an element

 

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

        var fruit:XML = <fruit name="Apple" color="red" />;
        
        trace( fruit.@*[0] );
        
        trace( fruit.@*[1] );
        
        trace( fruit.@*.length(  ) );
    }
  }
}



Use bracket to access an attribute by name, or when the attribute name contains characters not allowed in variable names

 

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

        var example:XML = <example bad-variable-name="yes" color12="blue" />;
        var num:Number = 12;
        
        trace( example.@["bad-variable-name"] );
        
        trace( example.@["color" + num] );
    }
  }
}



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

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var fruit:XML = <fruit name="Apple" color="red" />;
        
        trace( fruit.@color );

    }
  }
}



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