我想将<container>
节点下的子树作为xml获取到变量。我看了GPathResult
。但是没有办法找到任何办法。
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<container>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
</container>
</shiporder>
输出xml应该是
def xml= <item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
有人能指出我正确的方向吗?
答案 0 :(得分:1)
类似的东西:
import groovy.xml.XmlUtil
def mainXml =
'''
<shiporder orderid="889923"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="shiporder.xsd">
<orderperson>John Smith</orderperson>
<container>
<item>
<title>Empire Burlesque</title>
<note>Special Edition</note>
<quantity>1</quantity>
<price>10.90</price>
</item>
</container>
</shiporder>
'''
def slurper = new XmlSlurper().parseText(mainXml)
XmlUtil.serialize(slurper.container.item)