AS3数组转换XML

package {
	import flash.display.MovieClip;

	public class Test extends MovieClip {
		private var xmlMain:XML = <root></root>;
		private var arrData:Array = [["motion1","1"],["motion2","2"],["motion3","3"],["motion4","4"],["motion5","5"]];

		public function Test() {
			var xml:XML;
			var i:int = 0;
			for (i = 0; i < arrData.length; i++) {
				xml = <motion name={arrData[i][0]}>{arrData[i][1]}</motion>;
				xmlMain.appendChild(xml);
			}
			trace(xmlMain);

			xmlMain = <root></root>;
			for (i = 0; i < arrData.length; i++) {
				xml = <{arrData[i][0]}>{arrData[i][1]}</{arrData[i][0]}>;
				xmlMain.appendChild(xml);
			}
			trace(xmlMain);
		}
	}
}
//输出:
<root>
  <motion name="motion1">1</motion>
  <motion name="motion2">2</motion>
  <motion name="motion3">3</motion>
  <motion name="motion4">4</motion>
  <motion name="motion5">5</motion>
</root>

<root>
  <motion1>1</motion1>
  <motion2>2</motion2>
  <motion3>3</motion3>
  <motion4>4</motion4>
  <motion5>5</motion5>
</root>

你可能感兴趣的:(xml,数组,Flash,as3)