1 package com.suneee.scn.tms.rest.impl; 2 3 import org.dom4j.Document; 4 import org.dom4j.Element; 5 import org.dom4j.io.SAXReader; 6 7 import java.io.InputStream; 8 import java.io.OutputStream; 9 import java.io.OutputStreamWriter; 10 import java.net.URL; 11 import java.net.URLConnection; 12 import java.util.HashMap; 13 import java.util.Iterator; 14 import java.util.List; 15 import java.util.Map; 16 17 /** 18 * 类作用调用webservice得到天气预报服务 19 * @author qsw-Myonlystar 2010-1-13上午09:59:45 20 */ 21 public class Weather { 22 /** 23 * 获取soap请求头,并替换其中的标志符号为用户的输入符号 24 * @param city 用户输入城市名 25 * @return 用户将要发送给服务器的soap请求 26 */ 27 private static String getSoapRequest(String city) { 28 StringBuilder sb = new StringBuilder(); 29 sb.append("<?xml version="1.0" encoding="utf-8"?>" 30 + "<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" " 31 + "xmlns:xsd="http://www.w3.org/2001/XMLSchema" " 32 + "xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">" 33 + "<soap:Body> <getWeatherbyCityName xmlns="http://WebXml.com.cn/">" 34 + "<theCityName>" + city 35 + "</theCityName> </getWeatherbyCityName>" 36 + "</soap:Body></soap:Envelope>"); 37 return sb.toString(); 38 } 39 /** 40 * 用户把SOAP请求发送给服务器端,并返回服务器点返回的输入流 41 * @param city 用户输入的城市名称 42 * @return 服务器端返回的输入流,供客户端读取 43 * @throws Exception 44 */ 45 public static InputStream getSoapInputStream(String city) throws Exception { 46 try { 47 String soap = getSoapRequest(city); 48 if (soap == null) { 49 return null; 50 } 51 URL url = new URL( 52 "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx"); 53 URLConnection conn = url.openConnection(); 54 conn.setUseCaches(false); 55 conn.setDoInput(true); 56 conn.setDoOutput(true); 57 conn.setRequestProperty("Content-Length", Integer.toString(soap 58 .length())); 59 conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8"); 60 conn.setRequestProperty("SOAPAction", 61 "http://WebXml.com.cn/getWeatherbyCityName"); 62 OutputStream os = conn.getOutputStream(); 63 OutputStreamWriter osw = new OutputStreamWriter(os, "utf-8"); 64 osw.write(soap); 65 osw.flush(); 66 osw.close(); 67 InputStream is = conn.getInputStream(); 68 //System.out.println(is.toString()); 69 return is; 70 } catch (Exception e) { 71 e.printStackTrace(); 72 return null; 73 } 74 } 75 /** 76 * 通过dom4j对服务器端返回的XML进行解析 77 * @param city 用户输入的城市名称 78 * @return 符串 用,分割 79 */ 80 public static String getWeather(String city) { 81 Document document=null; 82 SAXReader reader = new SAXReader(); 83 String s=""; 84 Map map=new HashMap(); 85 map.put("design", "http://WebXml.com.cn/"); 86 reader.getDocumentFactory().setXPathNamespaceURIs(map); 87 try { 88 InputStream is = getSoapInputStream(city);//得到输入流 89 document=reader.read(is);//将输入流转化为document 90 String t =document.asXML(); 91 System.out.println(t); 92 } catch (Exception e) { 93 // TODO Auto-generated catch block 94 e.printStackTrace(); 95 } 96 97 98 99 /* List nodes = document.selectNodes("//design:string"); 100 for (Iterator it = nodes.iterator(); it.hasNext();) { 101 Element elm = (Element) it.next(); 102 String text=elm.getText(); 103 //System.out.println("fsffs"+text); 104 s=s+elm.getText()+" "; 105 }*/ 106 return s; 107 } 108 /** 109 * 测试函数 110 * @param args 111 */ 112 public static void main(String args[]){ 113 Weather w=new Weather(); 114 System.out.println(w.getWeather("北京")); 115 } 116 }