Groovy:Node.replaceNode 为 Node?
假设我有一个节点想要用 replaceNode
替换,但是,我不想使用 Builder
来完成它 - 或者更确切地说,我已经有了用来替换它的节点:
replacement = new XmlParser.parse('input.xml')
root.depthFirst().replaceme.each { it ->
it.replaceNode { node ->
// This is what I can't figure out
}
}
我已经尝试了很多不同的迭代,但似乎无法解决。如果我只返回该段中的文本,它将用空节点替换该节点。
例如,如果我的输入文件是这样的: 这个应该被替换
我有一个像这样的替代品: 这将取代原始
我想做的事情如下:
top = new XmlParser().parseFile('input.xml')
top.middle.each { it ->
it.replaceNode { node ->
new XmlParser().parseFile('replacement.xml')
}
}
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')
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不介意切换到 XmlSlurper() ,以下操作应该可以工作:
它将用 replacement.xml 的内容替换所有中间节点
If you don't mind switching to XmlSlurper() the following should work:
Which will replace all middle nodes with the contents of replacement.xml