SoapUI:从REST响应中提取XML节点的值

时间:2015-03-19 13:37:47

标签: xml groovy soapui

我发出休息请求,返回响应:

<OTA_DetailsRS EchoToken="" SequenceNmbr="1" Target="Production" TimeStamp="2015-03-19 13:42:45.08" Version="" xmlns="http://www.opentravel.org/OTA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Success/>
   <HotelDetails>
      <HotelDetail>
         <Code>10010</Code>
      </HotelDetail>
    </HotelDetails>
</OTA_DetailsRS>

我需要捕获该代码的值(10010)并在Groovy Script中进行定义。

我尝试了groovyUtils.getXmlHolder(“GetDetails #Response”)。getNodeValue(// SOME XPATH),但是NULL返回给我:(。我也尝试声明xmlns,但是失败了...有谁可以请告诉我,如何在groovy中获得10010?

谢谢你, 梅德

2 个答案:

答案 0 :(得分:3)

在SoapUI Groovy Script步骤中,您可以使用简单的:

def something = context.expand('${GetDetails#Response#//*:Code}')

如果右键单击Groovy脚本的正文并选择“获取数据”,该工具将帮助您构建这些内容。

答案 1 :(得分:0)

您可以在下方找到XmlSlurperXPath

的解决方案

<强>的XmlSlurper:

def xml='''
<OTA_DetailsRS EchoToken="" SequenceNmbr="1" Target="Production" TimeStamp="2015-03-19 13:42:45.08" Version="" xmlns="http://www.opentravel.org/OTA" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Success/>
   <HotelDetails>
      <HotelDetail>
         <Code>10010</Code>
      </HotelDetail>
    </HotelDetails>
</OTA_DetailsRS>
'''
def slurped = new XmlSlurper().parseText(xml)
assert slurped.HotelDetails.HotelDetail.Code == '10010'

<强>的XPath:

import javax.xml.xpath.*
import javax.xml.parsers.DocumentBuilderFactory


def xpath = XPathFactory.newInstance().newXPath()
def builder     = DocumentBuilderFactory.newInstance().newDocumentBuilder()
def inputStream = new ByteArrayInputStream( xml.bytes )
def parsed     = builder.parse(inputStream).documentElement
assert xpath.evaluate( '//HotelDetails/HotelDetail[1]/Code/text()' , parsed) == '10010'