package { import flash.display.Sprite; /** * @author:Frost.Yen * @E-mail:871979853@qq.com * @create: 2016-3-11 上午11:31:11 * */ public class OperateXML extends Sprite { private var _xml:XML = <data> <item>item1</item> <item>item2</item> <a>a</a> <b>b</b> </data>; public function OperateXML() { //delete _xml.item[0]; // 删除第一个item节点 //delete _xml.a;//删除a节点 var newNode:XML = <c>c</c>; //_xml = _xml.insertChildAfter(_xml.a,newNode);//在节点a后面插入新节点 //_xml = _xml.insertChildBefore(_xml.a,newNode);//在节点a前面插入新节点 _xml = _xml.insertChildBefore(_xml.item[0],newNode);//在节点a前面插入新节点 trace(_xml); //通过大括号运算符{}在构造XML时对变量进行引用 var arr:Array = [1,2,3,4,5]; var ab:Array = ["a","b"]; var xml:XML = new XML("<data></data>"); for(var i:int = 0;i<arr.length;i++){ var node:XML = new XML(); node = <item id={arr[i]}> <a>{ab[0]}</a> <b>{ab[1]}</b> </item>; xml.appendChild(node); } trace(xml); } } }