给定数据模型
<outerTag>
<tagA>
</tagB>
</tagA>
<tagC>
<interestingContent>
...
</interestingContent>
</tagC>
</outerTag> 我想将<interestingContent>的子节点移动到<tagB>中。我不知道要移动的节点的可能内容,它们可能也有孩子。我目前正在使用GPath,我想像这样简单的东西应该可以工作:
outertag.tagC.childNodes().each { node ->
outerTag.tagA.tagB.appendNode(node)
}但是,虽然我能够读取节点中的名称和文本,但appendNode似乎并没有做到这一点。虽然理论上我可以从子节点读取属性、文本和名称,使用它来创建新节点,并附加该节点,但我觉得这是不必要的补充,特别是因为它需要是一个递归函数,因为节点本身可以有子节点。
发布于 2019-01-16 18:44:38
对代码进行细微的更改
def outerTag = new XmlParser().parseText('''<outerTag>
<tagA>
<tagB/>
</tagA>
<tagC>
<interestingContent a="a">1</interestingContent>
<interestingContent a="b">2</interestingContent>
</tagC>
</outerTag>''')
outerTag.tagC[0].children().each { child ->
outerTag.tagA.tagB[0].append(child)
}
//reset value for tagC
outerTag.tagC[0].setValue("")
println groovy.xml.XmlUtil.serialize(outerTag)https://stackoverflow.com/questions/54212689
复制相似问题