簡體   English   中英

Groovy:如何在soapui 中替換現有xml 文件的Body 標記

[英]Groovy:How to replace Body tag for existing xml file in soapui

我正在嘗試將來自模擬請求的 XML 正文替換為現有的 XML 模式。 你能請任何人幫助我實現這一目標嗎

需要替換為 Base XML 文件的 XML 正文(取自 txt 文件):

<tns:Body xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/">
      <polsearch1:searchForPoliciesResponse xmlns:polsearch1="http://service.axa.de/komposit/policyretrieve/searchforpoliciesmodel/v1">
         <polsearch1:Entry Address="Colonia-Allee" AddressLine1="Colonia-Allee" City="Köln" EffectiveDate="2020-02-06T00:00:00+01:00" ExpirationDate="2021-02-06T00:00:00+01:00" InsuredName="Herr Dr. Axafiname Ovuir Suf" IsArchived="false" PolicyNumber="56000068476" PolicyType="VHV19Household" PostalCode="51067" ProducerCode="8834002000" Status="inforce" HouseNumber_De="10" DateOfBirth_De="1990-08-26T00:00:00+02:00"/>
      </polsearch1:searchForPoliciesResponse>
   </tns:Body>

我的基本 XML 文件:

<tns:Envelope xmlns:tns="http://schemas.xmlsoap.org/soap/envelope/">
   <tns:Header>
      some info;
   </tns:Header>
   <tns:Body>
      <polsearch1:searchForPoliciesResponse xmlns:polsearch1="http://service.axa.de/komposit/policyretrieve/searchforpoliciesmodel/v1">
         <polsearch1:Entry Address="Colonia-Allee" AddressLine1="Colonia-Allee" City="Köln" EffectiveDate="2019-02-27T00:01:00+01:00" ExpirationDate="2020-02-27T00:01:00+01:00" InsuredName="Mr. Testklasklsk Testqerqwerwqe" IsArchived="false" PolicyNumber="${#MockResponse#Request#declare namespace v1='http://service.axa.de/komposit/policyretrieve/searchforpoliciesmodel/v1'; //v1:searchForPolicies[1]/v1:criteria[1]/@PolicyNumber}" PolicyType="HR19LiabilityRetail" PostalCode="51067" ProducerCode="8834002000" Status="inforce" HouseNumber_De="88" DateOfBirth_De="1987-12-12T00:00:00+01:00"/>
      </polsearch1:searchForPoliciesResponse>
   </tns:Body>
</tns:Envelope>

我的 Groovy 腳本是這樣的:

jsonObjectForPartner = jsonSlurper.parseText(line) //line contains Body of xml from txt file 


String tempContent = new File(groovyUtils.projectPath+"/sp_baseFile.xml").getText('UTF-8') // reading Base file xml
    def Basefileholder = groovyUtils.getXmlHolder(tempContent) //taking into XMLholder class

//replacing the body content
Basefileholder.setNodeValue("//*:Body",jsonObjectForPolicy.line)

使用 setNodevalue 我可以放置字符串,但不會刪除現有內容。 任何人都可以幫忙嗎?

這個問題的答案是:

SearchPolicies.txt - 包含實際輸出(字符串格式的 xml 正文)

Sp_base.xml - 包含示例架構

任務:將 xml 正文內容從 Txt 文件替換為基礎 xml 文件

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
//Get Mockrequest
def requestholder = groovyUtils.getXmlHolder(mockRequest.requestContent)
//log.info(requestholder)
//get project root folder path
def projRootLocation = groovyUtils.projectPath
//log.info(projRootLocation)
//parent folder path
def requestSampleFileFolder = projRootLocation  
//Get the info of the Policy to be searched from request
def policynumber = requestholder.getNodeValue("//*:Body/*:searchForPolicies/*:criteria/@PolicyNumber")
//log.info(policynumber)

 //Check for the POLICY whethere existed in already created data
def allRowsDataForCreatedPolicies = new File(requestSampleFileFolder + "/SearchPolicies.txt").readLines()
def jsonObjectForPolicy 
def policyFound = false
import groovy.json.JsonSlurper
def jsonSlurper = new JsonSlurper()
allRowsDataForCreatedPolicies.find { String line ->  
  if(line.contains(policynumber))
  {
  log.info(line)      
  policyFound = true  
  jsonObjectForPolicy = jsonSlurper.parseText(line)
  true
  }
}
//Get base file
String tempContent = new File(groovyUtils.projectPath+"/sp_baseFile.xml").getText('UTF-8')
def Basefileholder = groovyUtils.getXmlHolder(tempContent)
def finalResponse
if(policyFound)
{
    //Removing the searchForPoliciesResponse node to replace with actual output 
      Basefileholder.removeDomNodes("//*:Body/*:searchForPoliciesResponse") 
      //Replacing the required body content to the base schema file
    Basefileholder.setNodeValue("//*:Body",jsonObjectForPolicy.XMLBodyResponse)
    //Removing unwanted charecters from xml
    finalResponse = Basefileholder.xml.replace("&lt;", "<")
    //Dsipatching the final response
    mockResponse.responseContent = finalResponse
}
else {mockResponse.responseContent = "Policy Number not found"}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM