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的請求頭,并替換其中的標(biāo)志符號為用戶輸入的城市
            *
            * 編寫者:王景輝
            *
            * @param city
            *            用戶輸入的城市名稱
            * @return 客戶將要發(fā)送給服務(wù)器的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請求發(fā)送給服務(wù)器端,并返回服務(wù)器點返回的輸入流
            *
            * 編寫者:王景輝
            *
            * @param city
            *            用戶輸入的城市名稱
            * @return 服務(wù)器端返回的輸入流,供客戶端讀取
            * @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;
            }
           }

           /**
            * 對服務(wù)器端返回的XML進(jìn)行解析
            *
            * 編寫者:王景輝
            *
            * @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("查詢結(jié)果為空!")) {
               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 周會 閱讀(4703) 評論(1)  編輯  收藏

          評論

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

          挺好的,請問,圖片信息是在網(wǎng)站下載嗎?  回復(fù)  更多評論   


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


          網(wǎng)站導(dǎo)航:
           
          <2011年1月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          303112345

          導(dǎo)航

          統(tǒng)計

          常用鏈接

          留言簿

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 曲麻莱县| 武川县| 报价| 富锦市| 屯昌县| 新泰市| 常山县| 开江县| 乌兰浩特市| 镇雄县| 深圳市| 读书| 澎湖县| 炎陵县| 南澳县| 田东县| 萨迦县| 大厂| 金秀| 财经| 新闻| 宁蒗| 清涧县| 洪雅县| 雷州市| 樟树市| 军事| 常宁市| 公主岭市| 天津市| 西乡县| 崇明县| 蒲城县| 蓝山县| 寿阳县| 赣州市| 镇安县| 绥芬河市| 英山县| 额敏县| 余姚市|