e4x - 对xml操作的一些示例


as3终于给了xml一个名分,使他成了真正的内置数据类型,
现在我们不必像以前一样,把xml转成数组或者object了,直接可以操作xml了
以下copy自as3 cookbook 第20章
[1b]写xml操作:[/1b]
var example:XML = ehbeesee;
[1b]用变量写:[/1b]
// Assume two variables exist, username and score
var username:String = "Darron";
var score:int = 1000;
// Use curly braces around the variable name to use its value when
// assigning XML via an XML literal
var example:XML =
 {username}
 {score}
 ;
[1b]字符串:[/1b]
// Create the XML structure with a string so use the value of both
// username and score inside of the XML packet.
var str:String = "" + username + ""
 + "" + score + "";
// Pass the string to the constructor to create an XML object
var example:XML = new XML( str );
[1b]填加元素[/1b]
// Create an XML instance to add elements to
var example:XML = ;
// Create a new XML node named newElement and add it to the
// example instance
example.newElement = ;
/* Displays:
 
 
 
*/
trace( example );
[1b]技巧:[/1b]
// Create an XML instance to work with
var example:XML = ;
var id:int = 10;
// Create a string to incorporate the value of id in the node name
example[ "user" + id ] = "";
/* Displays:
 
 
 
*/
trace( example );
[1b]“-” 会引起编译器错误,用数组操作符避免这个[/1b]
example.some-element = ""; // Generates a compiler error
example[ "some-element" ] = "";
[1b]insertChildBefore 和 insertChildAfter 作用[/1b]
// Create an XML instance to work with
var example:XML = ;
// Create an empty two element node
example.two = "";
// Before the two element node, add a one element node
example = example.insertChildBefore( example.two,  );
// After the two element node, add a three element node
example = example.insertChildAfter( example.two,  );
/* Displays:
 
 
 
*/
trace( example );
[1b]填加 文本节点 到xmlobject:[/1b]
// Create an XML instance to work with
var example:XML = ;
// Create a text node from a string
example.firstname = "Darron";
// Create a text node from a number
example.number = 24.9;
// Create a text node from a boolean
example.boolean = true;
// Create a text node from an array
example.abc = ["a", undefined, "b", "c", null, 7, false];
/* Displays:
 Darron
 24.9
 true
 a,,b,c,,7,false
*/
trace( example );
[1b]appendChild( ), prependChild( ), insertChildBefore( ), or insertChildAfter( ). 方法:[/1b]
// Create an XML instance to work with
var example:XML = ;
// Append a two element node containing a text node child
// with value 2
example.appendChild( 2 );
// Prepend a one element node containing a text node child
// with value "number 1"
example.prependChild( "number 1" );
// After the one element node, insert a text node with
// value 1.5
example.insertChildAfter( example.one[0], 1.5 );
// Before the two element node, insert a part element node
// containing a text node child with value 1.75
example.insertChildBefore( example.two[0], 1.75 );
/* Displays:
 "number 1"
本文转自:http://www.5uflash.com/flashjiaocheng/Flash-as3-jiaocheng/407.html

你可能感兴趣的:(C++,c,xml,C#,Flash)