SoapUI getting request parameters in mock service script

前端 未结 4 1759
忘掉有多难
忘掉有多难 2020-12-05 02:52

This is probably a very easy one for all SoapUI regulars.

In a SoapUI mock service response script, how do I extract the value inside the request I\'m replying to?<

4条回答
  •  旧时难觅i
    2020-12-05 03:29

    In a pure Java (not using SoapUI) you would just create a custom Naming Context like this one:

    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> getPrefixes(String namespace)
        {
            return null;
        }
    }
    

    Then, parse it like so:

    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  ); 
    }
    

提交回复
热议问题