Dmitry
Dmitry

Reputation: 694

SoapUI: Extract value of XML node from REST response

I fire rest request, which returns response:

<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>

I need to catch value of that code (10010) and def it in Groovy Script.

I tried groovyUtils.getXmlHolder( "GetDetails#Response").getNodeValue(//SOME XPATH), but NULL returns for me:(. Also I tried to declare that xmlns, but failed with it... Can anyone please tell me, how to get that 10010 in groovy?

Thank You, Dmitry

Upvotes: 1

Views: 4840

Answers (2)

SiKing
SiKing

Reputation: 10329

In SoapUI Groovy Script step you can use a simple:

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

If you right-click in the body of the Groovy Script and select Get Data, the tool will help you build these out.

Upvotes: 3

Opal
Opal

Reputation: 84786

Below you can find solution both with XmlSlurper and XPath

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'

Upvotes: 0

Related Questions