Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml SoapUI在模拟服务脚本中获取请求参数_Xml_Web Services_Groovy_Soapui - Fatal编程技术网

Xml SoapUI在模拟服务脚本中获取请求参数

Xml SoapUI在模拟服务脚本中获取请求参数,xml,web-services,groovy,soapui,Xml,Web Services,Groovy,Soapui,对于所有SoapUI常客来说,这可能是一个非常简单的问题 在SoapUI模拟服务响应脚本中,如何提取响应请求中的值 假设传入的请求 <ns1:foo> <ns3:data> <ns3:CustomerNumber>1234</ns3:CustomerNumber> </ns3:data> </ns1:foo> 1234 如何将“1234”放入Groovy变量中?我尝试使用xmlHolder,但似乎使用了

对于所有SoapUI常客来说,这可能是一个非常简单的问题

在SoapUI模拟服务响应脚本中,如何提取响应请求中的值

假设传入的请求

<ns1:foo>
  <ns3:data>
    <ns3:CustomerNumber>1234</ns3:CustomerNumber>
  </ns3:data>
</ns1:foo>

1234
如何将“1234”放入Groovy变量中?我尝试使用xmlHolder,但似乎使用了错误的XPath


(我已经知道如何设置属性并将其值集成到响应中。)

如果您想访问SOAP请求并执行一些XPath处理,由于和的强大功能,在soapUI中有一种更简单的方法来执行

以下是您访问客户号码的方式:

def req = new XmlSlurper().parseText(mockRequest.requestContent)
log.info "Customer #${req.foo.data.CustomerNumber}"
从Groovy 1.6.3(在soapUI 2.5及更高版本中使用)开始,XmlSlurper默认在名称空间感知和非验证模式下运行,因此无需执行其他操作

干杯
Shonzilla

还有一个例子:

def request = new XmlSlurper().parseText(mockRequest.requestContent)
def a = request.Body.Add.x.toDouble()
def b = request.Body.Add.y.toDouble()
context.result = a + b
在本例中,我们从请求中获取两个参数,并将它们转换为双精度参数。这样我们可以对参数进行计算。此示例的SoapUI响应示例为:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://example.org/math/types/">
   <soapenv:Header/>
   <soapenv:Body>
      <typ:AddResponse>
         <result>${result}</result>
      </typ:AddResponse>
   </soapenv:Body>
</soapenv:Envelope>

${result}
您可以看到计算结果是如何传递回响应的。

在纯Java(不使用SoapUI)中,您只需创建如下自定义命名上下文:

import java.util.Iterator;
import java.util.List;

import javax.xml.XMLConstants;
import javax.xml.namespace.NamespaceContext;

class WSNamespaceContext implements NamespaceContext
{
    public String getNamespaceURI(String prefix)
    {
        if ( prefix.equals("ns3") )
            return "http://www.mysite.com/services/taxservice";
       else if (prefix.equals("soapenv"))
            return "http://schemas.xmlsoap.org/soap/envelope/";
        else
            return XMLConstants.NULL_NS_URI;
    }

    public String getPrefix(String namespace)
    {
        if ( namespace.equals("http://www.mysite.com/services/taxservice") )
            return "ns3";
        else if (namespace.equals("http://schemas.xmlsoap.org/soap/envelope/"))
            return "soapenv";
        else
            return null;
    }

    public Iterator<List<String>> getPrefixes(String namespace)
    {
        return null;
    }
}
import java.util.Iterator;
导入java.util.List;
导入javax.xml.xmlstants;
导入javax.xml.namespace.NamespaceContext;
类WSNamespaceContext实现NamespaceContext
{
公共字符串getNamespaceURI(字符串前缀)
{
if(前缀等于(“ns3”))
返回“http://www.mysite.com/services/taxservice";
else if(前缀等于(“soapenv”))
返回“http://schemas.xmlsoap.org/soap/envelope/";
其他的
返回XMLConstants.NULL\u NS\u URI;
}
公共字符串getPrefix(字符串命名空间)
{
if(namespace.equals(“http://www.mysite.com/services/taxservice") )
返回“ns3”;
else if(namespace.equals(“http://schemas.xmlsoap.org/soap/envelope/"))
返回“soapenv”;
其他的
返回null;
}
公共迭代器getPrefixes(字符串命名空间)
{
返回null;
}
}
然后,像这样解析它:

XPathFactory factory = XPathFactory.newInstance(); 
XPath xp = factory.newXPath(); 
xp.setNamespaceContext( nsc ); 
XPathExpression xpexpr = xp.compile("//ns3:CustomerNumber/text()");
NodeList nodes = (NodeList)xpexpr.evaluate(doc, XPathConstants.NODESET); 
for ( int i = 0; i < nodes.getLength(); i++ )  { 
    String val = nodes.item(i).getNodeValue();
    System.out.println( "Value: " + val  ); 
}
XPathFactory=XPathFactory.newInstance();
XPath xp=factory.newXPath();
xp.setNamespaceContext(nsc);
XPathExpression xpexpr=xp.compile(“//ns3:CustomerNumber/text()”;
NodeList节点=(NodeList)xpexpr.evaluate(doc,xpathcontents.NODESET);
对于(inti=0;i
扩展并基于此,我提出了以下建议:

// Create XmlHolder for request content
def holder = new com.eviware.soapui.support.XmlHolder( mockRequest.requestContent )
holder.namespaces["ns3"] = "ns3"

// Get arguments
def custNo = holder.getNodeValue("//ns3:CustomerNumber")
context.setProperty("custNo", custNo)

谢谢你的例子!我喜欢Shonzilla和你的回复!这里有大量的XML-Q/A,但是如果您有一个JSON请求,那么它使用
def requestJson=new groovy.JSON.JsonSlurper().parseText(mockRequest.requestContent)
和例如
log.info“${requestJson.'CustomerNumber'}”也可以类似地工作