0

How do I read the nodename from a given XML response? I was using xmlSlurper in readyAPI Groovy editor but not able to get the values I wanted to fetch the ROOM,GENR values from rom1:RoomType Code from xml response.

def RoomTypes = new XmlSlurper().parseText(responseTestSuite1)

Sample XML as below

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
   <soap:Header/>
   <soap:Body>
      <rom1:GetRoomTypesListResponse xsi:schemaLocation="xsdlocation" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:rom1="service">
         <rom1:Success/>
         <rom1:Hotels>
            <rom1:Hotel HCode="ABSCD"/>
         </rom1:Hotels>
         <rom1:RoomTypes>
            <rom1:RoomType Code="ROOM">
               <rom1:Name Language="en">Guest room,  King or Queen or Double</rom1:Name>
            </rom1:RoomType>
            <rom1:RoomType Code="GENR">
               <rom1:Name Language="en">Guest room, 1 King</rom1:Name>
            </rom1:RoomType>
           </rom1:RoomTypes>
      </rom1:GetRoomTypesListResponse>
   </soap:Body>
</soap:Envelope>
Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38

1 Answers1

0

Here is what you need :

//Pass xml string in to below parseText method
println new XmlSlurper().parseText(xml).'**'.findAll { it.name() == 'RoomType'}*.@Code

Output

You get the list of values i.e., ROOM, GENR as output

You can quickly try this online Demo

If you are using ReadyAPI / SoapUI, use following Script Assertion instead of separate groovy script test step

//check if response is ok
assert context.response,'Resonse is empty'

def pXml = new XmlSlurper().parseText(context.response)
def codes = pXml.'**'.findAll { it.name() == 'RoomType' }*.@Code
log.info codes
Rao
  • 20,781
  • 11
  • 57
  • 77
  • @satishkumar, [Appreciate if you accept the solution as answered](http://stackoverflow.com/help/someone-answers). – Rao May 17 '17 at 05:02