A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© yting_xmei1129 中级黑马   /  2013-9-20 17:26  /  1281 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

有代码有真相、、、,在MyEclipse 8.5 下运行通过

package yting.weather;

import java.io.*;
import java.net.*;

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 {
        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();
        }

        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;
                }
        }

        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;
                }
        }

        public static void main(String[] args) {
                System.out.print(getWeather("衡阳"));
        }
}


2 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报

谢谢了,找了好久才找到的、、、
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马