栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java-工具-Webservice wsdl解析

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java-工具-Webservice wsdl解析

原文转载:https://www.cnblogs.com/coshaho/p/5689738.html

public class WsdlInfo
{
private String wsdlName;

private List interfaces;


public WsdlInfo(String path) throws Exception
{
    WProject project = new WProject();
    WsdlInterface[] wsdlInterfaces = Wsdlimporter.importWsdl( project, path );
    this.wsdlName = path;
    if(null != wsdlInterfaces)
    {    
        List interfaces = new ArrayList();
        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 getInterfaces() {
    return interfaces;
}

public void setInterfaces(List interfaces) {
    this.interfaces = interfaces;
}

}

public class InterfaceInfo
{
private String interfaceName;

private List operations;

private String[] adrress;

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

    this.adrress = wsdlInterface.getEndpoints();

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

    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 getOperations() {
    return operations;
}

public void setOperations(List 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());
        }
    }
}

}

第一中httpURLConnection方式调用

package com.ssh.webserviceTSY;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {
public static void main(String[] args) throws IOException {
//第一步:创建服务地址
URL url = new URL(“http://ws.webxml.com.cn/WebServices/MobileCodeWS.asmx?wsdl”);
//第二步:打开一个通向服务地址的连接
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//第三步:设置参数
//3.1发送方式设置:POST必须大写
connection.setRequestMethod(“POST”);
//3.2设置数据格式:content-type
connection.setRequestProperty(“content-type”, “text/xml;charset=utf-8”);
//3.3设置输入输出,因为默认新创建的connection没有读写权限,
connection.setDoInput(true);
connection.setDoOutput(true);

    //第四步:组织SOAP数据,发送请求  
    String soapXML = getXML("17321242779");  
    //将信息以流的方式发送出去
    OutputStream os = connection.getOutputStream();  
    os.write(soapXML.getBytes());  
    //第五步:接收服务端响应,打印  
    int responseCode = connection.getResponseCode();  
    if(200 == responseCode){//表示服务端响应成功  
        //获取当前连接请求返回的数据流
        InputStream is = connection.getInputStream();  
        InputStreamReader isr = new InputStreamReader(is);  
        BufferedReader br = new BufferedReader(isr);  
          
        StringBuilder sb = new StringBuilder();  
        String temp = null;  
        while(null != (temp = br.readLine())){  
            sb.append(temp);  
        }  
        
        
        System.out.println(sb.toString());  
        
        is.close();  
        isr.close();  
        br.close();  
    }  
    os.close();  
}  


public static String getXML(String phone){  
	
	String soapXML = ""  
	        +""  
	            +""  
	              +""    
	              +phone
	              +""  
	            +""  
	        +"";  
    return soapXML;  
}  

}

第二中httpURLConnection方式调用
package com.zyh.car.util;

import org.dom4j.document;
import org.dom4j.documentException;
import org.dom4j.documentHelper;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.dom4j.document;
import org.dom4j.documentException;
import org.dom4j.documentHelper;
import org.dom4j.Element;

import javax.annotation.Resource;

public class GetEquipMsgUtils {

public static void main(String[] args) throws IOException {
    //第一步:创建服务地址,也就是提供的WSDL的接口地址
    URL url = new URL("http://xxxxxxxxxxxxxxxx/xxxx/xxxxxxxxx?wsdl");
    //第二步:打开一个通向服务地址的连接
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    //第三步:设置参数
    //3.1发送方式设置:POST必须大写
    connection.setRequestMethod("POST");
    //3.2设置数据格式:content-type
    connection.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
    //3.3设置输入输出,因为默认新创建的connection没有读写权限,
    connection.setDoInput(true);
    connection.setDoOutput(true);

    //第四步:组织SOAP数据,发送请求,
    String soapXML = getXML(方法名,参数1,参数2);
    //将信息以流的方式发送出去
    OutputStream os = connection.getOutputStream();
    os.write(soapXML.getBytes());
    //第五步:接收服务端响应,打印
    int responseCode = connection.getResponseCode();
    if(200 == responseCode){//表示服务端响应成功
    //获取当前连接请求返回的数据流
        InputStream is = connection.getInputStream();
        InputStreamReader isr = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(isr);

        StringBuilder sb = new StringBuilder();
        String temp = null;
        while(null != (temp = br.readLine())){
            sb.append(temp);
        }
        或者
        int BUFFER_SIZE = 1024;

char[] buffer = new char[BUFFER_SIZE]; // or some other size,
int charsRead = 0;
while ( (charsRead = br.read(buffer, 0, BUFFER_SIZE)) != -1) {
sb.append(buffer, 0, charsRead);
}

        
        System.out.println("---"+sb.toString());
        try{
            //对返回的数据进行JSON格式化
            System.out.println(xml2Json(sb.toString()));
        }catch (Exception e){
            e.printStackTrace();
        }
        is.close();
        isr.close();
        br.close();
    }
    os.close();
}

//请求入参

public static String getXML(String method,int begin,int end){
String soapXML = “
+“soapenv:Header "
+“soapenv:Body "
+“tns:"+method+" “//方法名
+“tns:offset”+begin+””//参数1
+“tns:row_count”+end+””//参数2
+""
+""
+"
";
return soapXML;
}

public static JSonObject xml2Json(String xmlStr) throws documentException {
    document doc = documentHelper.parseText(xmlStr);
    JSonObject json = new JSonObject();
    dom4j2Json(doc.getRootElement(), json);
    return json;
}


public static void dom4j2Json(Element element, JSonObject json) {
    List chdEl = element.elements();
    for(Element e : chdEl){
        if (!e.elements().isEmpty()) {
            JSonObject chdjson = new JSonObject();
            dom4j2Json(e, chdjson);
            Object o = json.get(e.getName());
            if (o != null) {
                JSonArray jsona = null;
                if (o instanceof JSONObject) {
                    JSonObject jsono = (JSONObject) o;
                    json.remove(e.getName());
                    jsona = new JSonArray();
                    jsona.add(jsono);
                    jsona.add(chdjson);
                }
                if (o instanceof JSONArray) {
                    jsona = (JSONArray) o;
                    jsona.add(chdjson);
                }
                json.put(e.getName(), jsona);
            } else {
                if (!chdjson.isEmpty()) {
                    json.put(e.getName(), chdjson);
                }
            }
        } else {
            if (!e.getText().isEmpty()) {
                json.put(e.getName(), e.getText());
            }
        }
    }
}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/397138.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号