我创建了一个excel工作簿来保存多个soap请求的值。每个请求一张纸,每个请求将彼此不同。 groovy脚本将读取此工作表,该脚本使用excel中的数据发送肥皂请求。以下代码有效:
for (row in 2..rowSize-1)
{
for (col in 0..colSize-1)
{
if (col > 0)
{
if ((sheet1.getCell(col,1).getContents())==(sheet1.getCell(col-1,1).getContents()))
{
repeatFlag++
def currentCell = sheet1.getCell(col,1).getContents()
xpath = "//*:"+currentCell
repeatedElements.add(xpath)
}
else
repeatFlag = 0
log.info "repeat flag : ${repeatFlag}"
}
def tagName = sheet1.getCell(col,1).getContents()
tagCount = reqholder["count(//*:"+tagName+")"]
log.info "tag count for ${tagName} is : ${tagCount}"
if ( tagCount > 0 )
{
if( repeatFlag > 0)
{
currentNode = reqholder.getDomNode("//*:"+tagName)
use( DOMCategory )
{
parentNode = currentNode.parent()
alteredParents.add(parentNode)
}
def requestDoc = parentNode.getOwnerDocument()
newElement = requestDoc.createElementNS(parentNode.getNamespaceURI(), tagName)
log.info "current node : ${currentNode}"
log.info "parent node : ${parentNode}"
log.info "newElement node : ${newElement}"
parentNode.insertBefore(newElement, parentNode.getFirstChild())
reqholder.updateProperty()
}
def tagValue = sheet1.getCell(col,row).getContents()
reqholder.setNodeValue("//*:"+tagName,tagValue)
reqholder.updateProperty()
log.info "${tagName}"
}
}
testRunner.runTestStepByName(sheetName)
}
如果为一个节点添加了多个子元素(在这种情况下为CarrierRestriction),那么在进行下一次迭代之前,我将删除新添加的元素。这是为了保留原始的xml结构。以下是该代码段:
for (item in alteredParents)
{
log.info "Parent : ${item}"
for (def child; (child = item.getFirstChild())!= null; item.removeChild(child))
log.info "child ${child}"
reqholder.updateProperty()
}
以上提到的逻辑对于没有重复元素或嵌套的递归子节点的xml都适用。 我当前的Excel工作表如下所示
excel数据的示例xml如下:
<soapenv:Envelope>
<soap:Header xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken wsu:Id="UsernameToken-4" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<wsse:Username>********</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">****</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soap:Header>
<soapenv:Body>
<v1:addPatientCarrierRestrictionRequest>
<v1:requestHeader>
<v1:to>?</v1:to>
<v1:to>?</v1:to>
<v11:to>?</v11:to>
<v11:from>?</v11:from>
<v11:messageId>?</v11:messageId>
<!--Optional:-->
<v11:relatesToMessageId>?</v11:relatesToMessageId>
<v11:sentTime>?</v11:sentTime>
</v1:requestHeader>
<v1:patientId>?</v1:patientId>
<v1:carrierRestrictions/>
<temps/>
</v1:addPatientCarrierRestrictionRequest>
</soapenv:Body>
</soapenv:Envelope>
我无法使用现有逻辑在excel工作表中表示如下的嵌套元素。
<root>
<foo>
<element1>123</element1>
<element2>abc</element2>
</foo>
<foo>
<element1>345</element1>
<element2>qwe</element2>
</foo>
</root>
<foo>
的重复
子元素的值。