AS3操作XML添加节点

来源:互联网 发布:美国人工智能机器人 编辑:程序博客网 时间:2024/05/17 04:12

使用 prependChild() 方法或 appendChild() 方法可在 XML 对象属性列表的开头或结尾添加属性

var x1:XML = <p>Line 1</p> var x2:XML = <p>Line 2</p> var x:XML = <body></body>x = x.appendChild(x1);x = x.appendChild(x2);x = x.prependChild(<p>Line 0</p>); // x == <body><p>Line 0</p><p>Line 1</p><p>Line 2</p></body>

使用 insertChildBefore() 方法或 insertChildAfter() 方法在指定属性之前或之后添加属性

var xx:XML =
    <body>
        <p>Paragraph 1</p>
        <p>Paragraph 2</p>
    </body>;
var newNode:XML=<p>Paragraph 1.5</p>;
xx = xx.insertChildAfter(xx.p[0],newNode);
xx = xx.insertChildBefore(xx.p[2], <p>Paragraph 1.75</p>);
trace(xx);

还可以使用大括号运算符({})在构造 XML 对象时按引用(从其它变量)传递数据

var ids:Array = [121, 122, 123]; var names:Array = [["Murphy","Pat"], ["Thibaut","Jean"], ["Smith","Vijay"]]var x:XML = new XML("<employeeList></employeeList>");for (var i:int = 0; i < 3; i++){ var newnode:XML = new XML(); newnode = <employee id={ids[i]}> <last>{names[i][0]}</last> <first>{names[i][1]}</first> </employee>; x = x.appendChild(newnode)}
原创粉丝点击