java-工具-Webservice wsdl解析

原文链接:http://www.cnblogs.com/coshaho/p/5689738.html
wsdl解析

首先必然是理解第三方webservice的接口描述,也就是解析wsdl文件。wsdl文件是webservice服务接口描述文档,一个wsdl文件可以包含多个接口,一个接口可以包含多个方法。

public class WsdlInfo {
    private String wsdlName;

    private List<InterfaceInfo> interfaces;

    /** * coshaho * @param path wsdl地址 * @throws Exception */
    public WsdlInfo(String path) throws Exception
    {
        WProject project = new WProject();
        WsdlInterface[] wsdlInterfaces = WsdlImporter.importWsdl( project, path );
        this.wsdlName = path;
        if(null != wsdlInterfaces)
        {    
            List<InterfaceInfo> interfaces = new ArrayList<InterfaceInfo>();
            for(WsdlInterface wsdlInterface : wsdlInterfaces)
            {
                InterfaceInfo interfaceInfo = new InterfaceInfo(wsdlInterface);
                interfaces.add(interfaceInfo);
            }
            this.interfaces = interfaces;
        }
    }

    public String getWsdlName() {
        return wsdlName;
    }

    public void setWsdlName(String wsdlName) {
        this.wsdlName = wsdlName;
    }

    public List<InterfaceInfo> getInterfaces() {
        return interfaces;
    }

    public void setInterfaces(List<InterfaceInfo> interfaces) {
        this.interfaces = interfaces;
    }
}
public class InterfaceInfo 
{
    private String interfaceName;

    private List<OperationInfo> operations;

    private String[] adrress;

    public InterfaceInfo(WsdlInterface wsdlInterface)
    {
        this.interfaceName = wsdlInterface.getName();

        this.adrress = wsdlInterface.getEndpoints();

        int operationNum = wsdlInterface.getOperationCount();
        List<OperationInfo> operations = new ArrayList<OperationInfo>();

        for(int i = 0; i < operationNum; i++)
        {
            WsdlOperation operation = ( WsdlOperation )wsdlInterface.getOperationAt( i );
            OperationInfo operationInfo = new OperationInfo(operation);
            operations.add(operationInfo);
        }

        this.operations = operations;
    }

    public String getInterfaceName() {
        return interfaceName;
    }

    public void setInterfaceName(String interfaceName) {
        this.interfaceName = interfaceName;
    }

    public List<OperationInfo> getOperations() {
        return operations;
    }

    public void setOperations(List<OperationInfo> operations) {
        this.operations = operations;
    }

    public String[] getAdrress() {
        return adrress;
    }

    public void setAdrress(String[] adrress) {
        this.adrress = adrress;
    }
}
public class OperationInfo 
{
    private String operationName;

    private String requestXml;

    private String responseXml;

    public OperationInfo(WsdlOperation operation)
    {
        operationName = operation.getName();
        requestXml = operation.createRequest( true );
        responseXml = operation.createResponse(true);    
    }

    public String getOperationName() {
        return operationName;
    }

    public void setOperationName(String operationName) {
        this.operationName = operationName;
    }

    public String getRequestXml() {
        return requestXml;
    }

    public void setRequestXml(String requestXml) {
        this.requestXml = requestXml;
    }

    public String getResponseXml() {
        return responseXml;
    }

    public void setResponseXml(String responseXml) {
        this.responseXml = responseXml;
    }
}
public class WSDLParseTest 
{
    public static void main(String[] args) throws Exception
    {
        String url = "http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx?wsdl";
        WsdlInfo wsdlInfo = new WsdlInfo(url);
        System.out.println("WSDL URL is " + wsdlInfo.getWsdlName());

        for(InterfaceInfo interfaceInfo : wsdlInfo.getInterfaces())
        {
            System.out.println("Interface name is " + interfaceInfo.getInterfaceName());
            for(String ads : interfaceInfo.getAdrress())
            {
                System.out.println("Interface address is " + ads);
            }
            for(OperationInfo operation : interfaceInfo.getOperations())
            {
                System.out.println("operation name is " + operation.getOperationName());
                System.out.println("operation request is ");
                System.out.println("operation request is " + operation.getRequestXml());
                System.out.println("operation response is ");
                System.out.println(operation.getResponseXml());
            }
        }
    }
}
WSDL URL is http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx?wsdl
Interface name is ChinaOpenFundWSSoap12
Interface address is http://webservice.webxml.com.cn/WebServices/ChinaOpenFundWS.asmx
operation name is getFundCodeNameDataSet
operation request is 
operation request is <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
   <soap:Header/>
   <soap:Body>
      <web:getFundCodeNameDataSet/>
   </soap:Body>
</soap:Envelope>
operation response is 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <soap:Header/>
   <soap:Body>
      <web:getFundCodeNameDataSetResponse>
         <!--Optional:-->
         <web:getFundCodeNameDataSetResult>
            <xs:schema>
               <!--Ignoring type [{http://www.w3.org/2001/XMLSchema}schema]-->
            </xs:schema>
            <!--You may enter ANY elements at this point-->
         </web:getFundCodeNameDataSetResult>
      </web:getFundCodeNameDataSetResponse>
   </soap:Body>
</soap:Envelope>
operation name is getFundCodeNameString
operation request is 
operation request is <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
   <soap:Header/>
   <soap:Body>
      <web:getFundCodeNameString/>
   </soap:Body>
</soap:Envelope>
operation response is 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
   <soap:Header/>
   <soap:Body>
      <web:getFundCodeNameStringResponse>
         <!--Optional:-->
         <web:getFundCodeNameStringResult>
            <!--Zero or more repetitions:-->
            <web:string>?</web:string>
         </web:getFundCodeNameStringResult>
      </web:getFundCodeNameStringResponse>
   </soap:Body>
</soap:Envelope>
operation name is getOpenFundDataSet
operation request is 
operation request is <soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/">
   <soap:Header/>
   <soap:Body>
      <web:getOpenFundDataSet>
         <!--Optional:-->
         <web:userID>?</web:userID>
      </web:getOpenFundDataSet>
   </soap:Body>
</soap:Envelope>
operation response is 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://WebXml.com.cn/" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <soap:Header/>
   <soap:Body>
      <web:getOpenFundDataSetResponse>
         <!--Optional:-->
         <web:getOpenFundDataSetResult>
            <xs:schema>
               <!--Ignoring type [{http://www.w3.org/2001/XMLSchema}schema]-->
            </xs:schema>
            <!--You may enter ANY elements at this point-->
         </web:getOpenFundDataSetResult>
      </web:getOpenFundDataSetResponse>
   </soap:Body>
</soap:Envelope>

你可能感兴趣的:(java-工具-Webservice wsdl解析)