如何使用SOAP UI中的groovy在嵌套XML中提取标记的值

时间:2015-09-01 18:46:04

标签: xml groovy soapui

以下是我在SOAP UI中从SOAP请求获得的响应的一部分。

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

如何使用Groovy在soap UI Groovy Script步骤中获取所有Value标记下的Coinsurance标记文本?

3 个答案:

答案 0 :(得分:2)

另一种Groovy方法是:

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

其中xml是上述xml内容String

答案 1 :(得分:2)

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

答案 2 :(得分:0)

我不知道如何使用SoapUI将其挂钩,但您可以使用Groovy获取值标记:

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()
}

但是,SoapUI有一个不同的API,您可以在这里找到示例:

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

例如,迭代元素:

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]"
}