所以我有一个步骤需要一些数字ID&在数据库中查找其值。我希望能够编辑响应xml&包括从数据库中获取的值以便于阅读。
我想出了如何将xml节点添加到xmlHolder中,但是我似乎无法在节点中放置一个值。
这是最接近“看似”的工作代码:
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def dbResult = testRunner.testCase.testSteps["getNames"].getResponseContent()
def xmlOfferNames = groovyUtils.getXmlHolder(dbResult)
def names = xmlOfferNames.getNodeValues("//NAME")
def codes = xmlOfferNames.getNodeValues("//ID")
def recs = groovyUtils.getXmlHolder("Item#Reply")
def insert
for(int i=0; i<names.size(); i++){
for(node in recs.getDomNodes("//Item[ID='"+codes[i]+"']")){
//insert = '<NAME>'+names[i]+'</NAME>' //nothing is taking strings
//node.appendNode(new Node(node, 'NAME', names[i])) //dne constructor
insert = node.getOwnerDocument().createElementNS(node.getNamespaceURI(), "NAME")
//log.info names[i]
insert.setNodeValue(names[i])
//log.info insert.metaClass.methods*.name.sort().unique()
node.insertBefore(insert , node.getFirstChild())
}
}
recs.updateProperty()
log.info recs.getXml()
然而,当它打印出生成的xml时,我只有空<NAME/>
个标签,没有任何价值。
我尝试使用insert.setTextContent()
,但它说java.long.RuntimeException: DOM Level 3 Not implemented
。我没有看到任何可用于设置值的其他内容。
(半相关地说,奇怪的是我也没有在我的步骤中更新xml,但我还没有开始研究它)
答案 0 :(得分:1)
而不是使用holder
修改XML
尝试使用XmlSlurper
:
import groovy.xml.XmlUtil
def xml = '''<sample>
<RetailRecommendation>
<OfferCode>a1</OfferCode>
</RetailRecommendation>
<RetailRecommendation>
<OfferCode>b2</OfferCode>
</RetailRecommendation>
<RetailRecommendation>
<OfferCode>a1</OfferCode>
<OfferCode>ac3</OfferCode>
</RetailRecommendation>
</sample>
'''
def recs = new XmlSlurper().parseText(xml)
def names = ['kilo','mega','giga']
def codes = ['a1','b2','ac3']
// for each name
names.eachWithIndex{ name, i ->
// find all OfferCode elements inside RetailRecommendation
// where value is in codes[i] array
recs.RetailRecommendation.'**'.findAll { node ->
node.name() == 'OfferCode' && node.text() == codes[i]
}.each{ elem -> // for each element...
// create the new node to add
def nodeToAdd = new XmlSlurper().parseText('<OFFER_NAME>' + name + '</OFFER_NAME>')
// and append the node to your element
elem.parent().appendNode(nodeToAdd)
}
}
log.info XmlUtil.serialize(recs)
这导致:
<sample>
<RetailRecommendation>
<OfferCode>a1</OfferCode>
<OFFER_NAME>kilo</OFFER_NAME>
</RetailRecommendation>
<RetailRecommendation>
<OfferCode>b2</OfferCode>
<OFFER_NAME>mega</OFFER_NAME>
</RetailRecommendation>
<RetailRecommendation>
<OfferCode>a1</OfferCode>
<OfferCode>ac3</OfferCode>
<OFFER_NAME>kilo</OFFER_NAME>
<OFFER_NAME>giga</OFFER_NAME>
</RetailRecommendation>
</sample>
在您的代码中,您可以继续使用holder
来获取names
和codes
,但更改修改响应的方式,所有这些可能类似于:
import groovy.xml.XmlUtil
def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
def dbResult = testRunner.testCase.testSteps["getOfferNames"].getResponseContent()
def xmlOfferNames = groovyUtils.getXmlHolder(dbResult)
def names = xmlOfferNames.getNodeValues("//NAME")
def codes = xmlOfferNames.getNodeValues("//OFFERCODE1")
def recs = new XmlSlurper().parseText(context.expand('${getRetailRecommendation#Response}'))
// for each name
names.eachWithIndex{ name, i ->
// find all OfferCode elements inside RetailRecommendation
// where value is in codes[i] array
recs.RetailRecommendation.'**'.findAll { node ->
node.name() == 'OfferCode' && node.text() == codes[i]
}.each{ elem ->
// for each node found it
def nodeToAdd = new XmlSlurper().parseText('<OFFER_NAME>' + name + '</OFFER_NAME>')
elem.parent().appendNode(nodeToAdd)
}
}
log.info XmlUtil.serialize(recs)
希望这有帮助,
答案 1 :(得分:0)
事实证明,你不能只是在元素中添加一个字符串,你必须创建一个文本节点。
insert.appendChild(node.getOwnerDocument().createTextNode(names[i]))