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