我试图通过添加具有不同值的重复子节点来修改SOAP请求。
这是我到目前为止所做的:
请求:
<soapenv:Envelope xlmns:ns1 = "..." xlmns:ns2 = "..." xlmns:ns3 = "..." xlmns:ns4 = "...">
<soapenv:Body>
<ns2:OperationName>
<ns3:CuteN>77777</ns3:CuteN>
<ns3:RaveN>666</ns3:RaveN>
</ns2:OperationName>
</soapenv:Body>
</soapenv:Envelope>
请求修改为:(填写xml标记&amp;添加另一个标记<RaveN></RaveN>
及其值)
<soapenv:Envelope xlmns:ns1 = "..." xlmns:ns2 = "..." xlmns:ns3 = "..." xlmns:ns4 = "...">
<soapenv:Body>
<ns2:OperationName>
<ns3:CuteN>77777</ns3:CuteN>
<ns3:RaveN>666</ns3:RaveN>
<ns3:RaveN>888</ns3:RaveN>
</ns2:OperationName>
</soapenv:Body>
</soapenv:Envelope>
使用的代码:
/* Groovy Utilities Declaration */
def groovyUtils = new ns4.eviware.soapui.support.GroovyUtils(context)
/* Request Holder Setup */
def request = context.expand('${RequestStepName#Request}')
def requestHolder = groovyUtils.getXmlHolder("request")
/* Declaring Namespaces */
requestHolder.namespaces["soapenv"] = "http://schemas.xmlsoap.org/soap/envelope/"
requestHolder.namespaces["ns2"] = "..."
requestHolder.namespaces["ns3"] = "..."
requestHolder.namespaces["ns4"] = "..."
/* Set few string names */
def soapEnvXString = "//soapenv:Envelope"
def soapBodyXString = "/soapenv:Body"
def operXString = "/ns2:OperationName"
def raveNumXString = "/ns3:RaveNum"
/* create object of Request nodes */
def parentNode = requestHolder.getDomNode(soapEnvXString + soapBodyXString + operXString)
def reqRaveNumTwo = "888"
if(reqRaveNumTwo != null && reqRaveNumTwo != "")
{
/* create new node */
def secondRaveNumNode = new XmlSlurper(false,false).parseText("""<ns3:RaveNum>${reqRaveNumTwo}</ns3:RaveNum>""")
parentNode.appendNode(secondRaveNumNode)
}
/* Update the request holder properties */
requestHolder.updateProperty(true)
错误:
groovy.lang.MissingMethodException: No signature of method: org.apache.xmlbeans.impl.store.Xobj$ElementXobj.appendNode() is applicable for argument types: (groovy.util.slurpersupport.NodeChild) values: [888]
我知道我在这里遗漏了一些东西,这是我在Groovy深度潜水的第三天。任何帮助将不胜感激,提前致谢!
答案 0 :(得分:0)
我认为您的代码中存在两个问题,首先您要使用appendChild
方法,因为您尝试在<ns2:OperationName>
节点内添加子项(而不是appendNode
方法在您的对象中不存在),第二个问题是您将XmlSlurper
类与XmlBeans
混合,因为您无法将“节点”添加到从一个引擎到另一个引擎的对象中。
您需要更改if
语句中的代码以适应这些更正,因此您可以使用以下内容:
if(reqRaveNumTwo != null && reqRaveNumTwo != "")
{
/* create new node */
def nodeHolder = groovyUtils.getXmlHolder("<ns3:RaveNum xmlns:ns3=\"...\">${reqRaveNumTwo}</ns3:RaveNum>")
def itemNode = nodeHolder.getDomNode( "//ns3:RaveNum" )
// import the node
def secondRaveNumNode = parentNode.getOwnerDocument().importNode(itemNode,false)
// and finally append it
parentNode.appendChild(secondRaveNumNode)
}
而不是:
if(reqRaveNumTwo != null && reqRaveNumTwo != "")
{
/* create new node */
def secondRaveNumNode = new XmlSlurper(false,false).parseText("""<ns3:RaveNum>${reqRaveNumTwo}</ns3:RaveNum>""")
parentNode.appendNode(secondRaveNumNode)
}
使用XmlBeans
创建新节点的代码看起来不太直观,但这种方式有效:)
。
此外,我在您的代码中看到了这一点:def groovyUtils = new ns4.eviware.soapui.support.GroovyUtils(context)
,我想它只是typo
,但它必须是def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context)
希望这有帮助,
答案 1 :(得分:0)
代码更新如下,以使其实际运作:
/* Groovy Utilities Declaration */
def groovyUtils = new ns4.eviware.soapui.support.GroovyUtils(context)
/* Request Holder Setup */
def request = context.expand('${RequestStepName#Request}')
def requestHolder = groovyUtils.getXmlHolder("request")
/* Declaring Namespaces */
requestHolder.namespaces["soapenv"] = "http://schemas.xmlsoap.org/soap/envelope/"
requestHolder.namespaces["ns2"] = "..."
requestHolder.namespaces["ns3"] = "..."
requestHolder.namespaces["ns4"] = "..."
/* Set few string names */
def operXString = "//ns2:OperationName"
def raveNumXString = "//ns3:RaveNum"
/* create object of Request nodes */
def parentNode = requestHolder.getDomNode(operXString)
def reqRaveNumTwo = "888"
if(reqRaveNumTwo != null && reqRaveNumTwo != "")
{
/* create new node */
def nodeHolder = groovyUtils.getXmlHolder("<ns3:RaveNum xmlns:ns3=\"ns3\">${reqRaveNumTwo}</ns3:RaveNum>")
def itemNode = nodeHolder.getDomNode(raveNumXString)
// import the node
def secondRaveNumNode = parentNode.getOwnerDocument().importNode(itemNode,false)
// and finally append it
parentNode.appendChild(secondRaveNumNode)
}
/* Update the request holder properties */
requestHolder.updateProperty(true)
context.requestContent = requestHolder.xml