Reputation: 1079
Suppose I have a node I'd like to replace with replaceNode
, however, I don't want to use a Builder
to do it - or rather, I already have the node with which to replace it:
replacement = new XmlParser.parse('input.xml')
root.depthFirst().replaceme.each { it ->
it.replaceNode { node ->
// This is what I can't figure out
}
}
I've tried lots of different iterations, but can't seem to work it out. If I just return text in that segment, it replaces the node with an empty node.
For example, if my input file is this: This should get replaced
And I have a replacement like this: This will replace the Original
I'd like to do something like:
top = new XmlParser().parseFile('input.xml')
top.middle.each { it ->
it.replaceNode { node ->
new XmlParser().parseFile('replacement.xml')
}
}
Upvotes: 1
Views: 4601
Reputation: 3881
If you don't mind switching to XmlSlurper() the following should work:
def top = new XmlSlurper().parse('input.xml')
top.middle.each { node ->
node.replaceNode {
mkp.yield(new XmlSlurper().parse('replacement.xml'))
}
}
Which will replace all middle nodes with the contents of replacement.xml
Upvotes: 2