1

参考的例子

var x1:XML = <x1>
                <a id = "52">AYY</a>
                <a>AYY 2 </a>
                <b>BEE</b>
                <c>CEE</c>
            </x1>;

trace(x1.toXMLString());
trace("___________");

delete x1.a.@id;

trace(x1.toXMLString());
trace("___________");

delete x1.b;

trace(x1.toXMLString());
trace("___________");

delete x1.a;

trace(x1.toXMLString());

输出

<x1>
  <a id="52">AYY</a>
  <a>AYY 2</a>
  <b>BEE</b>
  <c>CEE</c>
</x1>
___________
<x1>
  <a>AYY</a>
  <a>AYY 2</a>
  <b>BEE</b>
  <c>CEE</c>
</x1>
___________
<x1>
  <a>AYY</a>
  <a>AYY 2</a>
  <c>CEE</c>
</x1>
___________
<x1>
  <c>CEE</c>
</x1>

如果我只想删除一个元素怎么办?或者我想删除没有子元素的元素?

我只能使用删除 x1.a。荒谬的!我花了几个小时找不到简单的方法。

var list:XMLList = x1.elements('a');
for each(var x:XML in list){
    if(....){
        //make something done
        //i want to delete this x from the xml object.while keep other node untouched.
    }
}

让我知道你处理这个问题的方式。

4

2 回答 2

2

您可以使用完整的 E4X 语法来匹配树中的节点并删除它们。例如这个:

var x1:XML =
    <x1>
        <a id="52">AYY</a>
        <a>AYY 2</a>
        <b>BEE</b>
        <c>CEE</c>
    </x1>;

trace(x1.toXMLString());

delete x1.a.(hasOwnProperty('@id') && @id=='52')[0];

trace('---');
trace(x1.toXMLString());

输出:

<x1>
  <a id="52">AYY</a>
  <a>AYY 2</a>
  <b>BEE</b>
  <c>CEE</c>
</x1>
---
<x1>
  <a>AYY 2</a>
  <b>BEE</b>
  <c>CEE</c>
</x1>
于 2012-10-27T15:02:02.857 回答
2

它很容易......看看:

var x1:XML = <x1>
                 <a id = "52">AYY</a>
                 <a>AYY 2 </a>
                 <b>BEE</b>
                 <c>CEE</c>
             </x1>;

trace(x1.toXMLString() + "\n");

var nodesToDelete:XMLList = x1.a;

trace(nodesToDelete.toXMLString() + "\n");

delete nodesToDelete[0];

trace(x1.toXMLString() + "\n");

输出:

<x1>
  <a id="52">AYY</a>
  <a>AYY 2</a>
  <b>BEE</b>
  <c>CEE</c>
</x1>

<a id="52">AYY</a>
<a>AYY 2</a>

<x1>
  <a>AYY 2</a>
  <b>BEE</b>
  <c>CEE</c>
</x1>

你甚至可以这样做:

delete x1.a[0];
于 2012-10-27T15:05:03.603 回答