组合和变换 XML 对象Flash Player 9 和更高版本,Adobe AIR 1.0 和更高版本 使用 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 x:XML =
<body>
<p>Paragraph 1</p>
<p>Paragraph 2</p>
</body>
var newNode:XML = <p>Paragraph 1.5</p>
x = x.insertChildAfter(x.p[0], newNode)
x = x.insertChildBefore(x.p[2], <p>Paragraph 1.75</p>)
如下面的示例所示,还可以使用大括号运算符({ 和 })在构造 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)
}
可以使用 = 运算符将属性指定给 XML 对象,如下所示: var x:XML =
<employee>
<lastname>Smith</lastname>
</employee>
x.firstname = "Jean";
x.@id = "239";
这将对 XML 对象 x 进行如下设置: <employee id="239">
<lastname>Smith</lastname>
<firstname>Jean</firstname>
</employee>
可以使用 + 和 += 运算符连接 XMLList 对象: var x1:XML = <a>test1</a> var x2:XML = <b>test2</b> var xList:XMLList = x1 + x2; xList += <c>test3</c> 这将对 XMLList 对象 xList 进行如下设置: <a>test1</a> <b>test2</b> <c>test3</c> |
|