Flash / Flex / ActionScript/XML/childIndex

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

Accessing Sibling Nodes

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK ISBN="0000000000">
            <TITLE>ActionScript</TITLE>                    <!--Previous sibling-->
            <AUTHOR>J, J</AUTHOR>
            <PUBLISHER>Books Ltd</PUBLISHER>  <!--Next sibling-->
          </BOOK>;
        
        var author:XML = novel.AUTHOR[0];
        // Previous sibling
        trace(author.parent().*[author.childIndex(  )-1]);  
        // Next sibling
        trace(author.parent().*[author.childIndex(  )+1]);  
    }
  }
}



Get previous Sibling from a giving xml data

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        previousSibling(someNode);
    }
    public function previousSibling (theNode:XML):XML {
      if (theNode.parent() != null && theNode.childIndex(  ) > 0) {
        return theNode.parent().*[theNode.childIndex(  )-1];
      } else {
        return null;
      }
    }
  }
}



Get the next sibling from a giving xml data

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        nextSibling(someNode);
    }
    public function nextSibling (theNode:XML):XML {
      if (theNode.parent(  ) != null
          && theNode.childIndex() < theNode.parent().children().length(  )-1) {
        return theNode.parent().*[theNode.childIndex(  )+1];
      } else {
        return null;
      }
    }
  }
}



look index of a child node up using childIndex()

 
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.movie[2].childIndex()); // Displays: 3
    }
  }
}