我试图将来自模拟请求的XML主体替换为现有XML模式。能否请任何人帮助我实现这一目标
需要替换为基本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我可以放置字符串,但是现有内容不会删除。有人可以帮忙吗?
答案 0 :(得分:0)
此问题的答案是:
SearchPolicies.txt-包含实际输出(字符串格式的xml正文)
Sp_base.xml-包含示例架构
任务:将Txt文件中的xml正文内容替换为基本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("<", "<")
//Dsipatching the final response
mockResponse.responseContent = finalResponse
}
else {mockResponse.responseContent = "Policy Number not found"}