Flash CS3 文档 |
|||
| ActionScript 3.0 编程 > 处理 XML > 组合和变换 XML 对象 | |||
使用 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)
}
可以使用 = 运算符将属性 (property) 和属性 (attribute) 赋予 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>
Flash CS3
当前页: http://livedocs.adobe.com/flash/9.0_cn/main/00000128.html