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

SoapUI: Extract value of XML node from REST response

我触发休息请求,returns 响应:

<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 脚本中对其进行定义。

我尝试了 groovyUtils.getXmlHolder( "GetDetails#Response").getNodeValue(//SOME XPATH),但对我来说是 NULL returns:(。我还尝试声明 xmlns,但失败了有了它...谁能告诉我,如何在 groovy 中获得 10010?

谢谢, 德米特里

您可以在下方找到 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'

在 SoapUI 中 Groovy 您可以使用简单的脚本步骤:

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

如果您右键单击 Groovy 脚本的主体并 select 获取数据,该工具将帮助您构建它们。