如何通过soapUI中的Groovy脚本读取节点名称

时间:2017-04-28 19:15:58

标签: groovy soapui ready-api

如何从给定的XML响应中读取节点名?我在readyAPI Groovy编辑器中使用xmlSlurper但无法获取值 我想从xml响应中的rom1:RoomType代码中获取ROOM,GENR值。

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

示例XML如下

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

1 个答案:

答案 0 :(得分:0)

以下是您的需求:

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

<强>输出

您会获得值列表,即ROOM, GENR作为输出

您可以在线快速尝试 Demo

如果您使用的是ReadyAPI / SoapUI,请使用以下Script Assertion而不是单独的groovy脚本测试步骤

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