20.10.删除元素,文本节点和属性

问题
我想删除XML对象中的元素节点,文本节点或属性
解决办法
使用delete 关键字
讨论
上面几节我们学习了如何添加元素,文本节点和属性到XML对象上。现在我们讨论如何删除这些节点,秘密就在于delete 关键字,看例子:
+展开
-ActionScript
var example:XML = <example>
<fruit color="Red">Apple</fruit>
<vegetable color="Green">Broccoli</vegetable>
<dairy color="White">Milk</dairy>
</example>;
// 删除fruit元素的color属性
delete example.fruit.@color;
// 删除dairy元素
delete example.dairy;
// 删除vegetable元素的文本节点
delete example.vegetable.text( )[0];
/* 显示:
<example>
<fruit>Apple</fruit>
<vegetable color="Green"/>
</example>
*/

trace( example );

delete只能一次删除一个节点,如果要删除多个节点,可通过for循环进行删除:
+展开
-ActionScript
var example:XML = <example>
<fruit color="red" name="Apple" />
</example>;
// Get an XMLList of the attributes for fruit
var attributes:XMLList = example.fruit.@*;
// Loop over the items backwards to delete every attribute.
// By removing items from the end of the array we avoid problems
// with the array indices changing while trying to loop over them.
for ( var i:int = attributes.length( ) - 1; i >= 0; i-- ) {
delete attributes[i];
}
/* 显示:
<example>
<fruit/>
</example>
*/

trace( example );

加支付宝好友偷能量挖...


评论(0)网络
阅读(79)喜欢(0)flash/flex/fcs/AIR