在 XMLParser Object Groovy 中真正删除节点 [英] Really deleting nodes in XMLParser Object Groovy

查看:17
本文介绍了在 XMLParser Object Groovy 中真正删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何通过 XMLParser 真正删除节点:

How to REALLY remove a node via XMLParser:

 x='''<X>
<A>
 <B c3='1'>
   <C1>a</C1>
   <C2>b</C2>
 </B>
 <B c3='2'>
   <C1>e</C1>
   <C2>e</C2>
 </B>
 <B c3='3'>
   <C1>f</C1>
   <C2>f</C2>
 </B>
</A>
</X>
'''

xml=new XmlParser().parseText(x)
def nodeToDel=xml.A.B.find{it.@C1='a'}
xml.remove(nodeToDel)
println xml
new XmlNodePrinter(new PrintWriter(new FileWriter(new File('c:/temp/a.xml')))).print(xml)

似乎工作但是!!!!当我将此翻译为我的问题时,它仍然保存原始 xml althoguh 在运行 remove-method 后返回 true.

Seems to work BUT!!!! as i translated this to my problem it still saves the original xml althoguh returning true after running remove-method.

我用谷歌搜索了一下,发现了这个 BUG.我现在似乎受到了影响.我该如何解决?有没有解决方法,或者我必须回到根目录并开始逐行复制它......??groovy 在这里真的很不合时宜吗:-/

I googled a little bit and found this BUG. And it seems as i am affected now of that. How can i solve it? Is there a workaround, or do i have to get back to the roots and start to copying it linewise...?? Is groovy really ungroovy here :-/

edit:如下所述,并从中获得经验,无法以这种方式删除等于e"的标签.只有第一条记录将被删除.我认为xml格式有问题.没有所需的格式:

edit: As written below, and got experience from that, it is not possible to remove the tag where equals 'e' this way. Only the first Record will be removed. I think there is a problem with the xml format. Not having the needed format:

<A x='1' y='2'></A>

并有它的格式

<代码><A><x>1</x><y>2</y></A>

有人能重现这个错误吗?

Is somebody able to reproduce this bug?

edit2:我使用的是 GroovyConsole 1.8.0.在示例中添加了 c3 属性.试图用同样的方法删除它,同样的错误:第一个 B 部分被删除了......现在最令人印象深刻的错误:用其他代码尝试过:

edit2: I am using the GroovyConsole 1.8.0. Added the c3 attributes to the example. Tried to remove it with same method, same bug: The first B section was removed... Now the most impressing bug: Tried it with other code:

def xml=new XmlParser().parseText(x)
xml.A.remove(xml.A.B.find{it.@'c3'= '3'}) //want to remove third section
new XmlNodePrinter(new PrintWriter(new FileWriter(new File('c:/temp/a.xml')))).print(xml)

结果第一节属性 c3 变为 3 ?!?!?!?!?呜呜呜……

results that the first section the property c3 is changed to 3 ?!?!?!?!? wtf...

我正在努力寻找一个星期的解决方案,这很累...

I am trying to find a solution for a week now, it is quite exhausting...

有人出主意吗?

推荐答案

删除节点的工作方式与其他 DOM API 非常相似.您必须将要删除的节点传递给其父节点的 remove 方法.

Removing nodes works pretty much like other DOM APIs. You have to pass the node you want to delete to the remove method of its parent.

= 运算符也是 Groovy 中的赋值运算符.it.@C1 = 'a' 会将 'a' 分配给每个 B 节点的 C1 属性文件.由于该赋值的结果是 'a',它被 Groovy 强制为 true,因此 find 将始终返回它遇到的第一个节点.

Also the = operator is the assignment operator in Groovy. it.@C1 = 'a' would assign 'a' to the C1 attribute of each B node in the document. Since the result of that assignment is 'a', which is coerced to true by Groovy, find will always return the first node it encounters.

xml=new XmlParser().parseText(x)
def nodeToDel=xml.A.B.C1.find { it.text() == 'a' }
def parent = nodeToDel.parent()
parent.remove(nodeToDel)

这篇关于在 XMLParser Object Groovy 中真正删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆