0

In Groovy, I want to delete a specific child node, but not all the child nodes.

Here is some example XML:

<A>
  <q>hello</q>
  <w>world</w>
  <e>again</e>
</A>

How do I delete the node and it's content/attributes (if any)?

I've tried with the .replaceBody method on the node, but child nodes aren't considered part of the body of the parent node.

Jonas Praem
  • 2,296
  • 5
  • 32
  • 53

1 Answers1

0

Here is an example with XmlParser that produces groovy.util.Node

And there is a method remove in it. http://docs.groovy-lang.org/latest/html/api/groovy/util/Node.html

def x = new XmlParser().parseText('''
<A>
  <q>hello</q>
  <w>world</w>
  <e>again</e>
</A>
''')

x.remove( x.w[0] )

println groovy.xml.XmlUtil.serialize(x)
daggett
  • 26,404
  • 3
  • 40
  • 56