使用 e4x 语法删除特定节点

发布于 2024-11-08 23:55:21 字数 906 浏览 0 评论 0 原文

我有这样的 XML 结构:

<numb>
  <variable>
   <name>john</name>
   <age>12</age>
 </variable>
 <variable>
   <name>janet</name>
   <age>10</age>
 </variable>
 <variable>
   <name>johanna</name>
   <age>22</age>
 </variable>
 <variable>
  <name>harry</name>
  <age>24</age>
 </variable>
</numb>

如果我尝试删除名为 johanna 的变量,我会这样做:

  delete numb.variable.(name=="johanna);

但是随后,我收到此错误:

 "Error #1119: Delete operator is not supported with operand of type XMLList"

建议 numb.variable.(name=="johanna") 是一个 XMLList ..但是后来我尝试了这个:

delete XML(numb.variable.(numb=="johanna"); 

但这并没有删除任何内容..

有人知道我如何删除麻木 XML 中的某个变量吗?

I have this XML structure:

<numb>
  <variable>
   <name>john</name>
   <age>12</age>
 </variable>
 <variable>
   <name>janet</name>
   <age>10</age>
 </variable>
 <variable>
   <name>johanna</name>
   <age>22</age>
 </variable>
 <variable>
  <name>harry</name>
  <age>24</age>
 </variable>
</numb>

If I try to delete the variable with name johanna I do this:

  delete numb.variable.(name=="johanna);

but then, I get this error:

 "Error #1119: Delete operator is not supported with operand of type XMLList"

suggesting the numb.variable.(name=="johanna") is an XMLList ..but then I tried this:

delete XML(numb.variable.(numb=="johanna"); 

but that didn't delete anything..

Anyone knows how I could delete a certain variable in the numb XML?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

命硬 2024-11-15 23:55:21

您可以使用 johanna 具有不同名称的子节点,并将它们设置为子节点。 com/en_US/FlashPlatform/reference/actionscript/3/XML.html#setChildren%28%29" rel="nofollow">setChildren() 方法。

例如,

var xml:XML = <numb>
  <variable>
   <name>john</name>
   <age>12</age>
 </variable>
 <variable>
   <name>janet</name>
   <age>10</age>
 </variable>
 <variable>
   <name>johanna</name>
   <age>22</age>
 </variable>
 <variable>
  <name>harry</name>
  <age>24</age>
 </variable>
</numb>

xml.setChildren(xml.variable.(name != 'johanna'));
trace(xml);
/*
Outputs:
<numb>
  <variable>
    <name>john</name>
    <age>12</age>
  </variable>
  <variable>
    <name>janet</name>
    <age>10</age>
  </variable>
  <variable>
    <name>harry</name>
    <age>24</age>
  </variable>
</numb>
*/

另一种选择是循环遍历满足条件的每个节点,并使用 childIndex() 方法:

简短版本:

for each (var match:XML in xml.variable.(name == 'johanna')) delete xml.variable[match.childIndex()];

较长版本:

var xml:XML = <numb>
  <variable>
   <name>john</name>
   <age>12</age>
 </variable>
 <variable>
   <name>janet</name>
   <age>10</age>
 </variable>
 <variable>
   <name>johanna</name>
   <age>22</age>
 </variable>
 <variable>
  <name>harry</name>
  <age>24</age>
 </variable>
</numb>;

var matches:XMLList = xml.variable.(name == 'johanna');
for each (var match:XML in matches) delete xml.variable[match.childIndex()];

You can retrieve the child nodes that have a different name from johanna and set those as the children nodes, using the setChildren() method.

e.g.

var xml:XML = <numb>
  <variable>
   <name>john</name>
   <age>12</age>
 </variable>
 <variable>
   <name>janet</name>
   <age>10</age>
 </variable>
 <variable>
   <name>johanna</name>
   <age>22</age>
 </variable>
 <variable>
  <name>harry</name>
  <age>24</age>
 </variable>
</numb>

xml.setChildren(xml.variable.(name != 'johanna'));
trace(xml);
/*
Outputs:
<numb>
  <variable>
    <name>john</name>
    <age>12</age>
  </variable>
  <variable>
    <name>janet</name>
    <age>10</age>
  </variable>
  <variable>
    <name>harry</name>
    <age>24</age>
  </variable>
</numb>
*/

Another option is to loop through each node that satisfies the condition and delete the corresponding node through array access notation using the childIndex() method:

short version:

for each (var match:XML in xml.variable.(name == 'johanna')) delete xml.variable[match.childIndex()];

longer version:

var xml:XML = <numb>
  <variable>
   <name>john</name>
   <age>12</age>
 </variable>
 <variable>
   <name>janet</name>
   <age>10</age>
 </variable>
 <variable>
   <name>johanna</name>
   <age>22</age>
 </variable>
 <variable>
  <name>harry</name>
  <age>24</age>
 </variable>
</numb>;

var matches:XMLList = xml.variable.(name == 'johanna');
for each (var match:XML in matches) delete xml.variable[match.childIndex()];
难如初 2024-11-15 23:55:21
numb.variable.(name=="johanna") 

返回一个 xml 元素的 xmllist,因此要删除它,您必须使用此语法

delete numb.variable.(name=="johanna)[0] as XML;
numb.variable.(name=="johanna") 

return an xmllist of one xml element, so to delete it you have to use this syntax

delete numb.variable.(name=="johanna)[0] as XML;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文