smiles
smiles

Reputation: 231

how to get attribute value for soapui response using groovy

my soap response:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <soap:Body>
      <GetInventoriesResponse xmlns="http://xxx">
         <GetInventoriesResult>
            <NewDataSet xmlns="">
               <Inventory attr1="1" attr2="101" />
               <Inventory attr1="1" attr2="101" />
            </NewDataSet>
         </GetInventoriesResult>
      </GetInventoriesResponse>
   </soap:Body>
</soap:Envelope>

I would like to read the values of attr1 from //Inventory[0]

What I am doing is

import groovy.sql.Sql  
import java.sql.DriverManager  
import java.sql.Connection  
import javax.sql.DataSource  
import java.sql.Driver;  
import java.util.*;  
import java.text.*; 

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )  
def responseHolder = groovyUtils.getXmlHolder( messageExchange.responseContent )

responseHolder.namespaces['ns4']='http://xxx'

def CounGetInventoriesResponse=responseHolder["count(//Inventory)"]
def CompanyId=responseHolder.getNodeValue("//namf:Inventory[0]/@attr1")

log.info "att1"+CompanyId

value of companyid is displayed as null

How do I resolve this . What is missing here

Upvotes: 0

Views: 478

Answers (1)

daggett
daggett

Reputation: 28634

https://www.w3schools.com/xml/xpath_syntax.asp

check predicates section in this article

Inventory[1] is a right way to reference first element

another issue - you are trying to access Inventory element with a namespace "//namf:Inventory when default namespace was reset to empty on the level of NewDataSet element: <NewDataSet xmlns="">

So, this should work

responseHolder.getNodeValue("//Inventory[1]/@attr1")

Upvotes: 2

Related Questions