JAVA處理含有DataSet的web services


本篇文章將講述如何通過axis取得.net發布的Web services,並處理返回含有DataSet的處理結果,展現在頁面上。
  涉及到的知識有:
  AXIS
        SAXBuilder
        xml

        步驟如下:
  1)、生成client stub文件。
     必備條件axis開源包。JDK
    A)、比如AXIS包放在你的C盤,則需要在環境變量當中設定的東東有:
                           AXIS_HOME=C:\AXIS
                           AXIS_CLASSPATH=.;%AXIS_HOME%\lib\axis.jar;%AXIS_HOME%\lib\axis-ant.jar;%AXIS_HOME%\lib\commons-discovery-0.2.jar;%AXIS_HOME%\lib\commons-logging-1.0.4.jar;%AXIS_HOME%\lib\jaxrpc.jar;%AXIS_HOME%\lib\saaj.jar;%AXIS_HOME%\lib\wsdl4j-1.5.1.jar;%AXIS_HOME%\lib\log4j-1.2.8.jar;%AXIS_HOME%\lib\activation.jar;%AXIS_HOME%\lib\mail.jar
                           然后加入到PATH當中:.;% AXIS_CLASSPATH%   (加入就可以了)
                          注意:很多人因為以上的配置不對,導致對AXIS學習的信心下降,其實,仔細一點就會發現在CLASSPATH當中找一找有沒有版本區別。也就是每一個JAR包是不是真的存在就可以了。(哈哈,我曾為之付出了兩天的時間娞!)
                 B)、進入DOS模式,轉到D盤,執行如下代碼:
      java org.apache.axis.wsdl.WSDL2Java -p <名稱,最好是包名> http://10.2.10.80/FocService/FocService.asmx?wsdl,看到了吧。對.net發布為asmx的文件后面加入?wsdl就可以解決了。
      然后就會在這個<名稱,最好是包名>下就會有一堆的JAVA文件,一般的話會有:
                       xxxx.java
                       xxxxLocator.java
                       xxxxSoap.java
                      xxxxSoapStub.java
                      其次還有以下划線_xxxx????.java的文件都是序列化的對象,也就是返回的結果對應元素。可以暫時不理它。下面就以作者生成的示例做一個引用webservice的操作。作者得到的文件如下:
                       SearchFlightInfo.java
                       SearchFlightInfoLocator.java
                       SearchFlightInfoSoap.java
                      SearchFlightInfoSoapStub.java
                       -----------------------------------------------------------------------
                       _GetFlightInfoResponse_GetFlightInfoResult.java
                      _GetstrFlightInfo.java
                     _GetstrFlightInfoResponse.java
                    C)、引用方法:
    

                               基本思想是從得到web services后取得他的主體信息(返回的數據信息),然后生成XML文件,然后再把XML文件轉換成一個JAVA序列化對象就可以了。
        如下是序列化對象的代碼。

try {   
            /**  
             * 取得前台的參數 1、航班日期 2、航班號  
             */  
            String planDate = request.getParameter("day");   
            String flyNo = request.getParameter("hanbanno");   
            /**  
             * 按照最新版本制作,最新位置在:http://10.2.10.62/OPCFltWS/SearchFlightInfo.asmx  
             */  
            SearchFlightInfo service = new SearchFlightInfoLocator();   
            SearchFlightInfoSoap client = service.getSearchFlightInfoSoap();   
            _GetFlightInfoResponse_GetFlightInfoResult FOCInfo=client.getFlightInfo(planDate, flyNo);   
            String ErrorInfo = client.getstrFlightInfo(planDate, flyNo);//錯誤警告信息
            MessageElement[] FOCElement = FOCInfo.get_any();//取得xml信息   
            List FOCElementHead = FOCElement[0].getChildren();//消息頭,DataSet對象   
            List FOCElementBody = FOCElement[1].getChildren();//消息體信息,DataSet對象 
            if (FOCElementBody.size() <= 0){   
                //無返回記錄,則無輸出   
                return mapping.findForward("NoRecord");    
            }   
            String nn = FOCElementBody.get(0).toString();//消息體的字符串形式    
            String path = request.getRealPath("");    
            CommonTools.saveXMLString(nn,path+"\\FOC\\heweiya1.xml");//保存為XML形式,其中FOC目錄做為固定目錄   
            /**  
                 * 使用DOM4J來解析這個序列化的對象  
                 */  
            //String path = request.getRealPath("");   
            Vector OutFoc = CommonTools.LoadXML(path + "\\FOC\\heweiya1.xml");   
            request.setAttribute("OutFoc", OutFoc);   
            if (request.getParameter("as") != null) {   
                return mapping.findForward("Foc1");   
            }   
            return mapping.findForward("Foc");   
        } catch (Exception e) {   
            /**  
             * 服務有異常,或者服務沒有啟動!  
             */  
            request.setAttribute("UserMsg", "Public.FocError");   
            return mapping.findForward("Fail");   
        }  
package com.travelsky.kams.front.foc.webservices;   
  
public class FocBean implements java.io.Serializable{   
  
    private String acown;//航班1   
    private String fltid;//航班2   
    private String datop;//查詢日期   
    private String std;   
    private String sta;   
    private String etd;//   
    private String eta;//   
    private String atd;   
    private String toff;//滑出時間   
    private String tdwn;//滑到位時間   
    private String ata;//飛行狀態   
    private String actyp;//機型   
    private String depstn;//起始機場   
    private String arrstn;//到達機場   
    private String plnid;//機號   
    /** default constructor */  
    public FocBean() {   
    }   
      
    public String getAcown() {   
        return this.acown;   
    }   
       
    public void setAcown(String acown) {   
        this.acown = acown;   
    }   
    public String getDatop() {   
        return this.datop;   
    }   
       
    public void setDatop(String datop) {   
        this.datop = datop;   
    }      
       
       
    public String getFltid() {   
        return this.fltid;   
    }   
       
    public void setFltid(String fltid) {   
        this.fltid = fltid;   
    }   
    public String getStd() {   
        return this.std;   
    }   
       
    public void setStd(String std) {   
        this.std = std;   
    }   
    public String getSta() {   
        return this.sta;   
    }   
       
    public void setSta(String sta) {   
        this.sta = sta;   
    }   
    public String getEtd() {   
        return this.etd;   
    }   
       
    public void setEtd(String etd) {   
        this.etd = etd;   
    }   
    public String getEta() {   
        return this.eta;   
    }   
       
    public void setEta(String eta) {   
        this.eta = eta;   
    }   
    public String getAtd() {   
        return this.atd;   
    }   
       
    public void setAtd(String atd) {   
        this.atd = atd;   
    }   
    public String getToff() {   
        return this.toff;   
    }   
       
    public void setToff(String toff) {   
        this.toff = toff;   
    }   
    public String getTdwn() {   
        return this.tdwn;   
    }   
       
    public void setTdwn(String tdwn) {   
        this.tdwn = tdwn;   
    }   
    public String getAta() {   
        return this.ata;   
    }   
       
    public void setAta(String ata) {   
        this.ata = ata;   
    }   
    public String getActyp() {   
        return this.actyp;   
    }   
       
    public void setActyp(String actyp) {   
        this.actyp = actyp;   
    }   
    public String getDepstn() {   
        return this.depstn;   
    }   
       
    public void setDepstn(String depstn) {   
        this.depstn = depstn;   
    }   
    public String getArrstn() {   
        return this.arrstn;   
    }   
       
    public void setArrstn(String arrstn) {   
        this.arrstn = arrstn;   
    }   
    public String getPlnid() {   
        return this.plnid;   
    }   
       
    public void setPlnid(String plnid) {   
        this.plnid = plnid;   
    }   
  
}   

 如下是解析XML文件變換成一個序列化的對象的JAVA代碼:(其中Vecto就相關於安全的LIST了)

/**  
     * 讀取XML文件所有信息  
     */  
    public static Vector LoadXML(String path) throws Exception {   
        Vector xmlVector = null;   
        FileInputStream fi = null;   
        try {   
            fi = new FileInputStream(path);   
            xmlVector = new Vector();   
            SAXBuilder sb = new SAXBuilder();   
            Document doc = sb.build(fi);   
            Element root = doc.getRootElement(); // 得到根元素   
            List focs = root.getChildren(); // 得到根元素所有子元素的集合   
            Element foc = null;   
            FocBean xml = null;   
            /**  
             * 從1開始,因為0為錯誤警告信息  
             */  
            for (int i = 1; i < focs.size(); i++) {   
                xml = new FocBean();   
                foc = (Element) focs.get(i); // 得到第一本書元素   
/*              xml.setAta(foc.getChild("ATA").getText());  
                xml.setAtd(foc.getChild("ATD").getText());  
                xml.setEta(foc.getChild("ETA").getText());  
                xml.setEtd(foc.getChild("ETD").getText());  
                xml.setFltid(foc.getChild("FLTID").getText());  
                xml.setSta(foc.getChild("STA").getText());  
                xml.setStd(foc.getChild("STD").getText());  
                xml.setTdwn(foc.getChild("TDWN").getText());  
                xml.setToff(foc.getChild("TOFF").getText());*/  
                /**  
                 * 如下是最新版本的取得實體對象的方法  
                 * modify by heweiya 2006-12-14  
                 */  
                xml.setAta(foc.getChild("ATA").getText());   
                xml.setAtd(foc.getChild("ATD").getText());   
                xml.setEta(foc.getChild("ETA").getText());   
                xml.setEtd(foc.getChild("ETD").getText());   
                xml.setFltid(foc.getChild("FLTID").getText());   
                xml.setSta(foc.getChild("STA").getText());   
                xml.setStd(foc.getChild("STD").getText());   
                xml.setTdwn(foc.getChild("TDWN").getText());   
                xml.setToff(foc.getChild("TOFF").getText());   
                xml.setAcown(foc.getChild("ACOWN").getText());   
                xml.setActyp(foc.getChild("ACTYP").getText());   
                xml.setArrstn(foc.getChild("ARRSTN").getText());   
                xml.setDatop(foc.getChild("DATOP").getText());   
                xml.setDepstn(foc.getChild("DEPSTN").getText());   
                xml.setPlnid(foc.getChild("Plnid").getText());   
                   
                xmlVector.add(xml);   
            }   
        } catch (Exception e) {   
            System.err.println(e + "error");   
        } finally {   
            try {   
                fi.close();   
            } catch (Exception e) {   
                e.printStackTrace();   
            }   
        }   
        return xmlVector;   
    }  

還有一個代碼如下:

// 把String 保存為文件   
    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();   
        }   
    }  

 以上所述是作者調試通過。使用STRUTS做為前台,然后輸出就可以了。本文章只討論技術問題,示例當中的代碼
只做為研究學習使用,並無它意。

 

轉:http://www.iteye.com/topic/39025


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM