webservice返回的xml怎么解析
2016-08-07 · 百度知道合伙人官方认证企业
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的版本
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<soap2.getPropertyCount();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();
}
数据格式如下:
2016-08-01
string content = HttpUtility.UrlEncode(content1.Value);
string postData = "title=" + title + "&content=" + content;
byte[] dataBytes = Encoding.UTF8.GetBytes(postData);
System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();
sw.Start();
string url = System.Configuration.ConfigurationManager.AppSettings["KeywordWebService"];
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = dataBytes.Length;
request.Method = "POST";
Stream postStream = request.GetRequestStream();
postStream.Write(dataBytes, 0, dataBytes.Length);
postStream.Close();
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
System.Xml.XmlDocument xml = new System.Xml.XmlDocument();
StreamReader receiveStream = new StreamReader(response.GetResponseStream());
string receiveString = receiveStream.ReadToEnd();
sw.Stop();
xml.LoadXml(receiveString);
System.Xml.XmlNodeList keywords = xml.SelectNodes("//Keyword");
string strkeywords = "";
foreach (System.Xml.XmlNode keyword in keywords)
{
System.Diagnostics.Debug.WriteLine(keyword.InnerText);
strkeywords = strkeywords + keyword.InnerText + " ";
}
this.txtSearch.Value = strkeywords.Trim();