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

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

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

Accessing Ancestors

 
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>;
        
        var title:XMLList = movieList.movie[1].title;
        var director:XMLList = title.parent().director;
        trace(title + "directed by "+ director);
    }
  }
}



A method form of this operator called descendants(). This function behaves the same way as the double dot:

 
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.descendants("year").text().toXMLString());
    }
  }
}



Descendants matches tags with the same name on different levels.

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