as3.0 Xml

as3终于给了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>;

字符串:
// 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 );

填加元素
// 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 );

技巧:
// 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 );

“-” 会引起编译器错误,用数组操作符避免这个
example.some-element = ""; // Generates a compiler error

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 );

填加 文本节点 到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 );

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 );

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 );
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值