您的位置:首页 > 其它

axis2远程调用webservice例子(返回xml用dom4j解析)

2015-08-23 23:35 477 查看
原文地址:axis2远程调用webservice例子(返回xml用dom4j解析)作者:苦咖啡

package test;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.soap.SOAP11Constants;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

//调用WebService 适用于车源,货源的删除
public class WebTest{

private static EndpointReference targetAirline = new EndpointReference(
"http://58.**.***.130/WebTest.asmx"); //这里是要调用的targetUrl

//设置发送请求的URL
//@param: param:参数类型 paramValue:参数值 method:方法名
//@return: 请求的URL
private static OMElement buildParam(String param,String paramValue,String method){
OMFactory fac = OMAbstractFactory.getOMFactory();
OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", ""); //http://tempuri.org/是命名空间
OMElement data = fac.createOMElement(method, omNs); //获得要调用的方法名
OMElement inner = fac.createOMElement(param, omNs); //获得该方法名要调用的参数名
inner.setText(paramValue); //输入参数
data.addChild(inner); //将该参数加入要调用的方法节点
return data;
}

// 设置调用的WebService地址
//@param: method:方法名
//@return: WebService地址
private static Options buildOptions(String method){
Options options = new Options();
options.setSoapVersionURI(SOAP11Constants.SOAP_ENVELOPE_NAMESPACE_URI);
options.setAction("http://tempuri.org/"+method); //设置调用的命名空间加方法
options.setTo(targetAirline);
options.setTransportInProtocol(Constants.TRANSPORT_HTTP); //设置传输协议
return options;
}

//获得返回数据(XML格式)
//@param: param:参数类型 paramValue:参数值 method:方法名
//@return:XML格式的string
public static String getResultByCode(String param,String paramValue,String method){
try{
ServiceClient sender = new ServiceClient();
sender.setOptions(buildOptions(method));
OMElement result = sender.sendReceive(buildParam(param,paramValue,method));
System.out.println("解析之前的数据:"+result.toString());
return result.toString();
}
catch(Exception e)
{
e.printStackTrace();
System.out.println("调用出错!");
return "调用出错!";
}

}

//dom4j解析WebService返回的数据
//@param: param:参数类型 paramValue:参数值 method:方法名
//@return: string类型的数据
public static String getResultByDom(String param,String paramValue,String method){
try {
Document doc= DocumentHelper.parseText(getResultByCode(param,paramValue,method));
Element root = doc.getRootElement();
Element rn=root.element(method+"Result"); //节点名
System.out.println("解析之后的数据:"+rn.getData());
return (String) rn.getData();
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("解析出错!");
return "解析出错!";
}
}

@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception {
WebTest web=new WebTest();
web.getResultByDom("ID","1781", "deleteCarsInfo"); //传入参数名,参数值,方法名

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: