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

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

问题描述

如何通过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.ABfind {it。@ C1 ='a'} $ b $ (新文件('c:/temp/a.xml'))))。print(xml)$ b xml.remove(nodeToDel)
println xml
new XmlNodePrinter(new PrintWriter(new FileWriter $ b

似乎可行但是!!!!
,因为我把它翻译成我的问题,它仍然保存了原始的xml althoguh,在运行remove-method后返回true。



我搜索了一下,发现< a href =http://jira.codehaus.org/browse/GROOVY-1832 =nofollow> BUG 。而且现在看来我受到了影响。我如何解决它?有没有解决方法,还是我必须回到根,并开始复制它linewise ...?在这里,groovy确实是ungroovy: - /



编辑 :如下所示,不可能通过这种方式去除等于'e'的标签。只有第一个记录将被删除。我认为这是XML格式的问题。没有所需的格式:

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

< A>的格式使用它, < X→1< / X> < Y> 2'; / Y> < / a>



有人能够重现此错误吗? edit2 :我正在使用GroovyConsole 1.8.0。向示例添加了c3属性。试图用相同的方法删除它,同样的错误:第一个B部分被删除...
现在最令人印象深刻的错误:用其他代码试过:

  def xml = new XmlParser()。parseText(x)
xml.A.remove(xml.ABfind {it。@'c3'='3'})/ /想删除第三部分
new XmlNodePrinter(new PrintWriter(new FileWriter(new File('c:/temp/a.xml'))))。print(xml)

导致第一部分属性c3变为3?!?!?!?!? wtf ...



我试图找到一个星期的解决方案,

有人提出了一个想法吗?

解决方案

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



另外 = 运算符是Groovy中的赋值运算符。 it。@ C1 ='a'会将'a'分配给 C1 <文档中每个 B 节点的code>属性。由于该赋值的结果是Groovy被强制为 true 'a',所以 find 总会返回遇到的第一个节点。

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


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)

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

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: 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>

and having it in the format

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

Is somebody able to reproduce this bug?

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)

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...

Somebody an idea?

解决方案

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.

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天全站免登陆