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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值