mo0206
mo0206

Reputation: 801

How to extract valuesof tags in a nested XML using groovy in SOAP UI

Below is a part of the response that I got from the SOAP request in SOAP UI.

<a:Bundle> 
  <a:Plans> 
    <a:Quotes> 
      <a:Quote>
        <a:StandardBenefits>
          <a:BenefitPeriod>
            <a:Description i:nil="true"/>
            <a:DisplayName i:nil="true"/>
            <a:Value>6 months</a:Value>
          </a:BenefitPeriod>
          <a:Coinsurance>
            <a:Description>50</a:Description>
            <a:DisplayName i:nil="true"/>
            <a:Value>50</a:Value>
          </a:Coinsurance>                                       
          <a:OutOfPocket>
            <a:Description>5000</a:Description>
            <a:DisplayName i:nil="true"/>
            <a:Value>5000</a:Value>
          </a:OutOfPocket>
          <a:PreventiveCare i:nil="true"/>
          <a:Rx i:nil="true"/>
          <a:StopLoss>
            <a:Description i:nil="true"/>
            <a:DisplayName i:nil="true"/>
            <a:Value>10000</a:Value>
          </a:StopLoss>
        </a:StandardBenefits>
      </a:Quote> 
      <a:Quote>
        //similar data like above quote
      </a:Quote>
    </a:Quotes> 
  </a:Plans> 
</a:Bundle>

How do I get the text of Value tags under all Coinsurance tags in soap UI Groovy Script step using Groovy?

Upvotes: 0

Views: 2475

Answers (3)

Eiston Dsouza
Eiston Dsouza

Reputation: 404

I think you are looking for this... Glad if it helps

// create an instance of GroovyUtils class
    def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

// define an xmlholder to detect a tag/node
    def holder = groovyUtils.getXmlHolder("YourRequest#response")

// the below line is used to get the values as per your requirement
// in your case,replace [*:parentNode] by  [a:Coinsurance] and [*:childNode] by [a:Value]

    log.info holder.getNodeValues("//*:parentNode//*:childNode").toString()

// if you want to get all the "Values" of the entire xml, just remove [//*:parentNode] from the above line of code

Upvotes: 2

dmahapatro
dmahapatro

Reputation: 50245

Another Groovy approach will be:

def slurped = new XmlParser(false, false).parseText(xml)
slurped.'**'.findAll { it.name() == 'a:Coinsurance' }*.'a:Value'*.text()

where xml is the above xml content as String.

Upvotes: 2

Aston
Aston

Reputation: 3712

I am not sure how to hook it in with SoapUI, but you can get the value tags with Groovy like this:

def xml = '...';

def rootE = new XmlParser().parseText(xml)

def values = rootE."a:Plans"."a:Quotes"."a:Quote".collect { quote ->
   return quote."a:StandardBenefits"."a:Coinsurance"."a:Value".text()
}

However, SoapUI has a different API, you can find examples here:

http://www.soapui.org/scripting---properties/tips---tricks.html#3-XML-nodes

For example, iterating through elements:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def holder = groovyUtils.getXmlHolder( "Request 1#Response" )
for( item in holder.getNodeValues( "//item" )) {
    log.info "Item : [$item]"
}

Upvotes: 0

Related Questions