var arr:Array = [1, 2, 3, 4, 5]; var xml:XML = new XML(<data></data>); for(var i:int = 0; i<arr.length; i++) { xml.appendChild(<id>{arr[i]}</id>); }
将<id> 标记的值设置为数组索引处的值并将该值追加到XML 对象:
+展开
-ActionScript
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 < ids.length; 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) }