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读取完成,本人测试正常,如有不妥之处,请留言。

你可能感兴趣的:(apache,jdk,.net,webservice,ext)