在Groovy中查找XML元素

时间:2016-02-18 22:59:28

标签: xml groovy

如何在XML soap响应中按名称(不是通过引用,如:first(); last(); parent())查找元素?

<xyzOrderReturn>
  <xyzOrderResponse>
    <UlidList>
        <Ulid ExtnUlid="1-222-333">
            <OrderList TotalRecords="0"/>
        </Ulid>
    </UlidList>
  </xyzOrderResponse>
</xyzOrderReturn>

这是我为'xyzOrderResponse'尝试的,但没有一个正在运作...

  def result = new XmlSlurper().parseText(xml)

  def element1 = result.breadthFirst()*.findAll { it.name() == 'xyzOrderResponse' }
  def element2 = result.depthFirst().findAll { it.name() == 'xyzOrderResponse' }

     println element1
     println element2

1 个答案:

答案 0 :(得分:2)

这两项工作,它们只是在您打印列表时不显示,但列表的大小为1并且:

def result = new XmlSlurper().parseText(xml)

def element1 = result.breadthFirst().findAll { it.name() == 'xyzOrderResponse' }
def element2 = result.depthFirst().findAll { it.name() == 'xyzOrderResponse' }

println element1*.name()
println element2*.name()

打印

[xyzOrderResponse]
[xyzOrderResponse]

您还可以将depthFirst变量替换为:

def element2 = result.'**'.findAll { it.name() == 'xyzOrderResponse' }