java解析soap

          package com.arj.comm.util;

          import java.io.InputStream;
          import java.io.OutputStream;
          import java.io.OutputStreamWriter;
          import java.net.URL;
          import java.net.URLConnection;
          import javax.xml.parsers.DocumentBuilder;
          import javax.xml.parsers.DocumentBuilderFactory;
          import org.w3c.dom.Document;
          import org.w3c.dom.Node;
          import org.w3c.dom.NodeList;

          public class WeatherReport {
           /**
            * 獲取SOAP的請求頭,并替換其中的標志符號為用戶輸入的城市
            *
            * 編寫者:王景輝
            *
            * @param city
            *            用戶輸入的城市名稱
            * @return 客戶將要發送給服務器的SOAP請求
            */
           private static String getSoapRequest(String city) {
            StringBuilder sb = new StringBuilder();
            sb
              .append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"
                + "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" "
                + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
                + "xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
                + "<soap:Body>    <getWeatherbyCityName xmlns=\"http://WebXml.com.cn/\">"
                + "<theCityName>" + city
                + "</theCityName>    </getWeatherbyCityName>"
                + "</soap:Body></soap:Envelope>");
            return sb.toString();
           }

           /**
            * 用戶把SOAP請求發送給服務器端,并返回服務器點返回的輸入流
            *
            * 編寫者:王景輝
            *
            * @param city
            *            用戶輸入的城市名稱
            * @return 服務器端返回的輸入流,供客戶端讀取
            * @throws Exception
            */
           private static InputStream getSoapInputStream(String city) throws Exception {
            try {
             String soap = getSoapRequest(city);
             if (soap == null) {
              return null;
             }
             URL url = new URL(
               "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx");
             URLConnection conn = url.openConnection();
             conn.setUseCaches(false);
             conn.setDoInput(true);
             conn.setDoOutput(true);

             conn.setRequestProperty("Content-Length", Integer.toString(soap
               .length()));
             conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
             conn.setRequestProperty("SOAPAction",
               "http://WebXml.com.cn/getWeatherbyCityName");

             OutputStream os = conn.getOutputStream();
             OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8");
             osw.write(soap);
             osw.flush();
             osw.close();

             InputStream is = conn.getInputStream();
             return is;
            } catch (Exception e) {
             e.printStackTrace();
             return null;
            }
           }

           /**
            * 對服務器端返回的XML進行解析
            *
            * 編寫者:王景輝
            *
            * @param city
            *            用戶輸入的城市名稱
            * @return 字符串 用,分割
            */
           public static String getWeather(String city) {
            try {
             Document doc;
             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
             dbf.setNamespaceAware(true);
             DocumentBuilder db = dbf.newDocumentBuilder();
             InputStream is = getSoapInputStream(city);
             doc = db.parse(is);
             NodeList nl = doc.getElementsByTagName("string");
             StringBuffer sb = new StringBuffer();
             for (int count = 0; count < nl.getLength(); count++) {
              Node n = nl.item(count);
              if(n.getFirstChild().getNodeValue().equals("查詢結果為空!")) {
               sb = new StringBuffer("#") ;
               break ;
              }
              sb.append(n.getFirstChild().getNodeValue() + "#\n");
             }
             is.close();
             return sb.toString();
            } catch (Exception e) {
             e.printStackTrace();
             return null;
            }
           }
           /**
            * 測試用
            * @param args
            * @throws Exception
            */
           public static void main(String[] args) throws Exception {
            System.out.println(getWeather("長沙"));
            System.out.println("po&oi".split("&").length) ;
            System.out.println("##".split("#").length) ;
           }
          }

          posted on 2011-01-27 16:11 周會 閱讀(4702) 評論(1)  編輯  收藏

          評論

          # re: java解析soap 2013-05-31 12:57 楚原

          挺好的,請問,圖片信息是在網站下載嗎?  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          <2013年5月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678

          導航

          統計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 抚松县| 苏尼特左旗| 丹巴县| 甘洛县| 凌云县| 德兴市| 泗洪县| 开封市| 固阳县| 南城县| 德钦县| 普洱| 屏南县| 佛山市| 沾益县| 策勒县| 宁陕县| 马山县| 大英县| 庄浪县| 息烽县| 永福县| 新田县| 利辛县| 栾城县| 平乡县| 祁连县| 永新县| 霸州市| 贵定县| 扎赉特旗| 哈密市| 克东县| 广宁县| 周至县| 清河县| 临海市| 新蔡县| 彭州市| 工布江达县| 古交市|