Flash / Flex / ActionScript/XML/XML Element

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

Accessing Text Nodes

 
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.TITLE.children()[0]);  // A reference to the text node ActionScript
    }
  }
}



Access XML elements by index

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var novel:XML = <BOOK>
            <AUTHOR>J, J</AUTHOR>
            <AUTHOR>S, S</AUTHOR>
          </BOOK>;
        
        trace(novel.AUTHOR[0]);
        trace(novel.AUTHOR[1]);
    }
  }
}



Assign XML element value

 

package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var elementName:String = "BOOK";
        var novel:XML = <{elementName}/>;
        
        
        trace(novel);
    }
  }
}



Change the element"s value

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

        var novel:XML = <BOOK>
            <AUTHOR>J, J</AUTHOR>
            <AUTHOR>S, S</AUTHOR>
          </BOOK>;
        novel.AUTHOR[0].setName("WRITER");
        
        trace(novel);
        
    }
  }
}



Create a new element by creating a property on the XML instance and assigning it a value

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:XML = <example />;
        // Create a new property emptyElement and assign it an empty string value
        example.emptyElement = "";
        
        trace( example );
    }
  }
}



Use the dot operator (.) on the XML instance and add the element

 
package{
  import flash.display.Sprite;
  
  public class Main extends Sprite{
    public function Main(){
        var example:XML = <example />;
        // Create a new XML node named newElement and add it to the example instance
        example.newElement = <newElement />;
        trace( example );
    }
  }
}