webservice返回的xml怎么解析

2025-02-26 15:04:58
推荐回答(3个)
回答1:

一般来说,调用webService通常需要几个步骤,在调用之前,首先需要下载Soap的jar包。

1、参数设置:上面说到的几个参数都要先设置,这主要依赖于要调用的web'Service的网址:

// 命名空间      

String nameSpace = "http://WebXml.com.cn/";      

// 调用的方法名称      

String methodName = "getDetailInfoByTrainCode";      

// EndPoint      

String endPoint = "http//webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx";      

// SOAP Action      

String soapAction = "http//WebXml.com.cn/getDetailInfoByTrainCode";     

2、指定命名空间与调用方法名

// 指定WebService的命名空间和调用的方法名      

SoapObject rpc = new SoapObject(nameSpace, methodName);    

3、设置参数:

// 设置需调用WebService接口需要传入的两个参数TrainCode、userId      

rpc.addProperty("TrainCode", params[0]);      

rpc.addProperty("UserID","");   

4、生成调用WebService方法的SOAP请求信息

// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本      

SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER12);   

envelope.bodyOut = rpc;   

5、调用WebService方法

try {      

// 调用WebService      

transport.call(soapAction, envelope);      

} catch (Exception e) {      

e.printStackTrace();      

}     

6、解析WebService中的DataSet数据

SoapObject soap1=(SoapObject)object.getProperty("getDetailInfoByTrainCodeResult");  

SoapObject childs=(SoapObject)soap1.getProperty(1);  

SoapObject soap2=(SoapObject)childs.getProperty(0);  

///  

for(int i=0;i

SoapObject soap3=(SoapObject)soap2.getProperty(i);  

///  

Info info=new Info();  

info.setStation(soap3.getProperty(0).toString());  

info.setArriveTime(soap3.getProperty(1).toString());  

info.setStartTime(soap3.getProperty(2).toString());  

info.setKm(soap3.getProperty(3).toString());  

Raininfo.add(info);  

//result=soap3.getProperty(3).toString();  

}  

数据格式如下:

回答2:

写个xml工具类处理

回答3:

webservice返回的xml解析方法:
一般来说,调用webService通常需要几个步骤,在调用之前,首先需要下载Soap的jar包。
1、参数设置:上面说到的几个参数都要先设置,这主要依赖于要调用的web'Service的网址:
// 命名空间
String nameSpace = "http://WebXml.com.cn/";
// 调用的方法名称
String methodName = "getDetailInfoByTrainCode";
// EndPoint
String endPoint = "http//webservice.webxml.com.cn/WebServices/TrainTimeWebService.asmx";
// SOAP Action
String soapAction = "http//WebXml.com.cn/getDetailInfoByTrainCode";
2、指定命名空间与调用方法名
// 指定WebService的命名空间和调用的方法名
SoapObject rpc = new SoapObject(nameSpace, methodName);
3、设置参数:
// 设置需调用WebService接口需要传入的两个参数TrainCode、userId
rpc.addProperty("TrainCode", params[0]);
rpc.addProperty("UserID","");
4、生成调用WebService方法的SOAP请求信息
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本