如何在groovy中的特定节点下获取xml

时间:2014-03-11 12:53:26

标签: xml groovy

我想将<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>

有人能指出我正确的方向吗?

1 个答案:

答案 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)