2014-01-08 47 views
0
在soapUI中,

上了SoapUI響應使用XMLParser的

嗨使用XmlParser的

麻煩,我想用「XML解析器」,以驗證在soapUI中,我的XML響應。

我一直在玩弄這在Groovy腳本,如果我聲明我可以訪問標記,並指定我的XML Groovy的腳本中,像這樣

如果我聲明在腳本中的XML這工作..

def xml = """ 
<NS1:createShipmentResponse xmlns:NS1="http://www.royalmailgroup.com/api/ship/V1"> 
     <NS1:integrationHeader> 
      <dateTime xmlns="http://www.royalmailgroup.com/integration /core/V1">2013-12-24T22:20:34</dateTime> 
      <version xmlns="http://www.royalmailgroup.com/integration/core/V1">1</version> 
      <identification xmlns="http://www.royalmailgroup.com/integration/core/V1"> 
       <applicationId>111111113</applicationId> 
       <transactionId>420642961</transactionId> 
      </identification> 
     </NS1:integrationHeader> 
     <NS1:completedShipmentInfo> 
      //xml not complete, other info in here. 
     </NS1:completedShipmentInfo> 
     <NS1:integrationFooter> 
      <warnings xmlns="http://www.royalmailgroup.com/integration/core/V1"> 
       <warning> 
        <warningCode>W0022</warningCode> 
        <warningDescription>The customerReference specified is longer than 12 characters and has been truncated</warningDescription> 
       </warning> 
       <warning> 
        <warningCode>W0026</warningCode> 
        <warningDescription>The departmentReference specified is invalid and  will be ignored</warningDescription> 
       </warning> 
      </warnings> 
     </NS1:integrationFooter> 
     </NS1:createShipmentResponse> 
""" 

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

parser.'NS1:integrationFooter'.warnings.warning.warningCode.each{ 
    log.info it.text() 
} 

但它似乎沒有正在運行的測試實例中工作時,我從SOAP響應的實例變量XMLPARSER如下。

def response  = context.expand('${createShipment_v04#Response}'); 

我知道解析器變量已分配XML響應,因爲當我可以把它打印到日誌..

即log.info解析器打印...

Wed Jan 08 16:33:38 GMT 2014:INFO:{http://schemas.xmlsoap.org/soap/envelope /}Envelope[attributes={}; value=[{http://schemas.xmlsoap.org/soap/envelope/}Body[attributes={}; value=[{http://www.royalmailgroup.com/api/ship/V1}createShipmentResponse[attributes={}; value=[{http://www.royalmailgroup.com/api/ship/V1}integrationHeader[attributes={}; ....... 

但是當我從soap響應實例化xmlParser請求時,下面的代碼不會打印任何東西。

parser.'NS1:integrationFooter'.warnings.warning.warningCode.each{ 
     log.info it.text() 
} 

任何幫助將不勝感激。

回答

0

我相信你在錯誤的層面上工作。

parser.Body ...。

+0

嗨。感謝您的迴應。但我已經嘗試過這一點,似乎沒有任何工作,即parser.Body.'NS1:createShipmentResponse'。'NS1:integrationFooter'.warnings.warning.warningCode.each {... – chucknor

0

好的。事實證明,我不需要'NS1:'部分。下面的作品..

slurper.Body.createShipmentResponse.integrationFooter.warnings.warning.warningCode.each{ 
    log.info it.text() 
} 
0

下面應該工作:

def response = context.expand('${createShipment_v04#Response}'); 
def parser = new XmlSlurper().parseText(response) 


def warningCodes = parser.'**'.findAll { 
    it.name()=='warningCode' 
} 

warningCodes.each { 
    log.info it 
}