分類  >  Web前端 >

WebService處理.net中Dataset有關問題解決方法

tags:    時間:2013-12-10 21:47:32
WebService處理.net中Dataset問題解決辦法
  在此我用的是Asix框架來解決的,在Asix包下有一個叫作WSDL2Java的類,可用來解析WSDL以生成Java代碼供程序員使用,我嘗試過用soap來處理Dataset,不過沒有成功(浪費了不少時間)。
  首先下載Asix包,直接到Apache下就可以了,下載完成後將解壓的webroot/lib下的包全部拷到JDK的lib目錄下。然後由控制台進入Jdk目錄,運行:
Java -Djava.ext.dirs=lib org.apache.axis.wsdl.WSDL2Java http://www.webxml.com.cn/WebServices/TrainTimeWebService.asmx?wsdl
紅色部分是你要的文件調用的webservice程序的地址,就是上面說到的那個wsdl文件,當然前提你必須配置好了Java的環境變數,接下來你會看到在Jdk目錄下生成出來了一個cn(適個人設置而定)文件夾,這裡面就是Asix為我們生成的Java代碼。
  然後將文件夾拷入你的項目的src目錄下即可。生成出來的代碼中我主要使用的是TrainTimeWebServiceLocator,TrainTimeWebServiceSoap,GetDetailInfoByTrainCodeResponseGetDetailInfoByTrainCodeResult這幾個類,
  首先將信息封裝到一個Pojo類里,
public class Train { 	private String trainStation;  	private String arriveTime;  	private String startTime;  	private String km; 	public String getArriveTime() { 		return arriveTime; 	} 	public void setArriveTime(String arriveTime) { 		this.arriveTime = arriveTime; 	} 	public String getKm() { 		return km; 	} 	public void setKm(String km) { 		this.km = km; 	} 	public String getStartTime() { 		return startTime; 	} 	public void setStartTime(String startTime) { 		this.startTime = startTime; 	} 	public String getTrainStation() { 		return trainStation; 	} 	public void setTrainStation(String trainStation) { 		this.trainStation = trainStation; 	}   } 

 然後處理信息,
public class TrainInfo { 	public ArrayList getInfo(String traincode) { 		ArrayList trainList = null; 		try { 			TrainTimeWebServiceLocator service = new TrainTimeWebServiceLocator(); 			TrainTimeWebServiceSoap client = service.getTrainTimeWebServiceSoap(); 			GetDetailInfoByTrainCodeResponseGetDetailInfoByTrainCodeResult traininfo =  client.getDetailInfoByTrainCode(traincode, ""); 			MessageElement[] traElt = traininfo.get_any(); 			List trainElementHead = traElt[0].getChildren();//消息頭,DataSet對象     	        List trainElementBody = traElt[1].getChildren();//消息體信息,DataSet對象   	        if (trainElementBody.size() <= 0){     	            //無返回記錄,則無輸出     	            return null; 	        }                 String nn = trainElementBody.get(0).toString();//消息體的字元串形式                 saveXMLString(nn,"train.xml");             trainList = loadXML("train.xml");             /*for(Iterator iter=trainList.iterator(); iter.hasNext();) {             	Train train = (Train)iter.next();             	System.out.println(train.getArriveTime());             }*/ 	             		} catch(Exception e) { 			e.printStackTrace(); 		} 		return trainList; 	} 	 	public static void saveXMLString(String XMLString, String fileName) throws IOException {     		File file = new File(fileName);     		if (file.exists()) {     		    file.delete();     		}     		file.createNewFile();     		if (file.canWrite()) {     		    FileWriter fileOut = new FileWriter(file);     		    fileOut.write(XMLString);     		    fileOut.close();     		}     	} 	 	 public static ArrayList loadXML(String path) throws Exception { 		 //Train train = null; 		 ArrayList<Train> trainList = null; 		 FileInputStream fi = null; 		 try { 			 fi = new FileInputStream(path); 			 trainList = new ArrayList<Train>(); 			 SAXBuilder sb = new SAXBuilder();     			 Document doc = sb.build(fi);     			 Element root = doc.getRootElement(); // 得到根元素     			 List focs = root.getChildren(); 			 Element foc = null; 			 Train train = null; 			  			 for(int i=0; i<focs.size(); i++) { 				 train = new Train(); 				 foc = (Element) focs.get(i); 				 train.setArriveTime(foc.getChild("ArriveTime").getText()); 				 train.setKm(foc.getChild("KM").getText()); 				 train.setStartTime(foc.getChild("StartTime").getText()); 				 train.setTrainStation(foc.getChild("TrainStation").getText()); 				  				 trainList.add(train); 			 } 		 } catch (Exception e) { 			 e.printStackTrace(); 		 } 		 return trainList; 	 } 	 public static void main(String[] args) { 		 new TrainInfo().getInfo("1132"); 	 } } 

到此可將Dataset讀取完成,本人測試正常,如有不妥之處,請留言。

推薦閱讀文章

Bookmark the permalink ,來源:互聯網