2015-09-01 44 views
0

下面是我從SOAP UI中的SOAP請求中獲取的響應的一部分。如何使用SOAP UI中的groovy提取嵌套XML中的標記值

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

我如何獲得的Value標籤下使用Groovy肥皂UI Groovy腳本步驟中的所有Coinsurance標籤的文本?

回答

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

另一個Groovy的方法將是:

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

其中xml是上面的XML內容作爲String

0

我認爲你正在尋找這個......很高興,如果有幫助

// 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 
相關問題