Flash / Flex / ActionScript/XML/insert

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

Add a new child to the end of the existing xml data

 
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>
        
        
        novel.*[novel.*.length(  )-1] += <DESCRIPTION>A modern classic</DESCRIPTION>;
        
        trace(novel);
    }
  }
}



Adding a new child after all existing children

 
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>
        
        novel.insertChildAfter(novel.*[novel.*.length(  )-1],
                               <DESCRIPTION>A modern classic</DESCRIPTION>);
        
        trace(novel);
    }
  }
}



Adding a new child after a specific existing child

 
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>
        
        novel.insertChildAfter(novel.AUTHOR[0], <AUTHOR>D, V</AUTHOR>);
        
        trace(novel);
    }
  }
}



Adding a new child before a specific existing child

 
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>
        
        novel.insertChildBefore(novel.AUTHOR[0], <PRICE>19.99</PRICE>);
        
        trace(novel);
    }
  }
}



As an alternative to the insertChildAfter( ) approach (1)

 
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>
        
        novel.AUTHOR[0] = novel.AUTHOR[0] + <AUTHOR>D, V</AUTHOR>;
        
        trace(novel);
    }
  }
}



As an alternative to the insertChildAfter( ) approach (2)

 
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>
        
        novel.AUTHOR[0] += <AUTHOR>D, V</AUTHOR>;
        
        trace(novel);
    }
  }
}



Use appendChild method to add new child to the end of existing xml data

 
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>
        
        novel.appendChild(<DESCRIPTION>A modern classic</DESCRIPTION>);
        
        trace(novel);
    }
  }
}