package com.test.commons;/*** java获取新浪天气预报代码*/
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;/*** 解析xml文档,包括本地文档和url**/
public class CommonsWeatherUtils {InputStream inStream;Element root;public InputStream getInStream() {return inStream;}public void setInStream(InputStream inStream) {this.inStream = inStream;}public Element getRoot() {return root;}public void setRoot(Element root) {this.root = root;}public CommonsWeatherUtils() {}/*** 通过输入流来获取新浪接口信息* @param inStream*/public CommonsWeatherUtils(InputStream inStream) {if (inStream != null) {this.inStream = inStream;DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();try {DocumentBuilder domBuilder = domfac.newDocumentBuilder();Document doc = domBuilder.parse(inStream);root = doc.getDocumentElement();} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}public CommonsWeatherUtils(String path) {InputStream inStream = null;try {inStream = new FileInputStream(path);} catch (FileNotFoundException e1) {e1.printStackTrace();}if (inStream != null) {this.inStream = inStream;DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();try {DocumentBuilder domBuilder = domfac.newDocumentBuilder();Document doc = domBuilder.parse(inStream);root = doc.getDocumentElement();} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}public CommonsWeatherUtils(URL url) {InputStream inStream = null;try {inStream = url.openStream();} catch (IOException e1) {e1.printStackTrace();}if (inStream != null) {this.inStream = inStream;DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance();try {DocumentBuilder domBuilder = domfac.newDocumentBuilder();Document doc = domBuilder.parse(inStream);root = doc.getDocumentElement();} catch (ParserConfigurationException e) {e.printStackTrace();} catch (SAXException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}}}/*** * @param nodes* @return 单个节点多个值以分号分隔*/public Map<String, String> getValue(String[] nodes) {if (inStream == null || root==null) {return null;}Map<String, String> map = new HashMap<String, String>();// 初始化每个节点的值为nullfor (int i = 0; i < nodes.length; i++) {map.put(nodes[i], null);}// 遍历第一节点NodeList topNodes = root.getChildNodes();if (topNodes != null) {for (int i = 0; i < topNodes.getLength(); i++) {Node book = topNodes.item(i);if (book.getNodeType() == Node.ELEMENT_NODE) {for (int j = 0; j < nodes.length; j++) {for (Node node = book.getFirstChild(); node != null; node = node.getNextSibling()) {if (node.getNodeType() == Node.ELEMENT_NODE) {if (node.getNodeName().equals(nodes[j])) {String val = node.getTextContent();String temp = map.get(nodes[j]);if (temp != null && !temp.equals("")) {temp = temp + ";" + val;} else {temp = val;}map.put(nodes[j], temp);}}}}}}}return map;}public static void main(String[] args) {String link="http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0";try {System.out.println(URLEncoder.encode("重庆", "GBK"));} catch (UnsupportedEncodingException e1) {// TODO Auto-generated catch blocke1.printStackTrace();}URL url;try {url = new URL(link);CommonsWeatherUtils parser = new CommonsWeatherUtils(url);String[] nodes = {"city","status1","temperature1","status2","temperature2"};Map<String, String> map = parser.getValue(nodes);System.out.println(map.get(nodes[0])+" 今天白天:"+map.get(nodes[1])+" 最高温度:"+map.get(nodes[2])+"℃ 今天夜间:"+map.get(nodes[3])+" 最低温度:"+map.get(nodes[4])+"℃ ");} catch (MalformedURLException e) {e.printStackTrace();}}
}

java获取新浪天气预报代码相关推荐

  1. 调用实现天气预报功能android,Android编程实现获取新浪天气预报数据的方法

    本文实例讲述了Android编程实现获取新浪天气预报数据的方法.分享给大家供大家参考,具体如下: 新浪天气预报地址: http://php.weather.sina.com.cn/xml.php?ci ...

  2. 新浪天气预报代码及城市代码

    名称:新浪天气预报代码 代码 :<IFRAME ID='ifm2' WIDTH='260' HEIGHT='70' ALIGN='CENTER' MARGINWIDTH='0' MARGINHE ...

  3. 2022年新浪股票接口更新需要加Referer才能获取数据, java获取新浪股票数据 http://hq.sinajs.cn

    新浪股票 2022年更新后 java获取数据 引用 <dependency><groupId>cn.hutool</groupId><artifactId&g ...

  4. 【Java】获取新浪股票接口,并且发到qq邮箱以及钉钉群

    Java获取新浪股票接口并发送邮箱和钉钉机器人 提示:这里可以添加系列文章的所有文章的目录,目录需要自己手动添加 例如:第一章 Python 机器学习入门之pandas的使用 -利用java获取新浪股 ...

  5. 利用libcurl获取新浪股票接口

    参考 利用libcurl获取新浪股票接口, ubuntu和openwrt实验成功(三) http://blog.chinaunix.net/uid-27194309-id-3987565.html 利 ...

  6. 使用Pull解析XML获取新浪新闻

    目标是获取新浪新闻如图所示位置的头条新闻数据: 思路是这样的,先访问这个首页拿到这个部分每一条新闻的url,然后再逐一访问这些详情页面,从详情页面获取标题正文图片等数据. 1.通过HttpUrlCon ...

  7. java 利用新浪天气API获取天气预报

    新浪为我们提供了天气预报获取接口API  http://php.weather.sina.com.cn/xml.php?city=武汉&password=DJOYnieT8234jlsK&am ...

  8. Java 模拟新浪登录 2016

    想学习一下网络爬虫,涉及到模拟登录,查阅了一番资料以后发现大部分都有点过时了,就使用前辈们给的经验,Firefox抓包调试,采用httpclient模拟了一下新浪登录.不对之处多多包含.需要的可以用浏 ...

  9. Java模拟新浪和腾迅自动登录并发送微博(2013年3月更新可用)

    1.准备工作 只是登录无需申请新浪和腾迅的开发者账号,如果需要发送微博功能,需要申请一个新浪和腾迅的开发者账号,并添加一个测试应用. 过程请参考官方帮助文档,申请地址:新浪:http://open.w ...

最新文章

  1. 一文看懂深度学习模型压缩和加速
  2. 数据库-ADONET-使用强类型DataSet
  3. 企业网络推广专员浅析企业网络推广日常维护要做好
  4. 蛋壳公寓再获5亿美元C轮融资,老虎环球基金、蚂蚁金服联合领投
  5. 排序算法【稳定性+空间复杂度+时间复杂度(平均、最好、最坏)】
  6. 第十九天:实施定性风险分析
  7. 熬夜肝完这份Framework笔记,已拿到offer
  8. 2017级面向对象程序设计 作业二
  9. 百度SEO站群小旋风蜘蛛池站群X8模板
  10. windows安装java环境
  11. SQL Server 内存泄露(memory leak)——游标导致的内存问题
  12. 在MFC中调用DLL .
  13. 爬楼梯 · Climbing Stairs
  14. Bailian2940 求和【进制+迭代】
  15. 关于mac打开互联网下载应用提示的解决方法
  16. windows7计算机不显示u盘,Win7系统U盘文件不显示的三种解决方法
  17. 太平洋服务器cpu型号,Intel正式发布:新一代6W的超低功耗平台CPU
  18. Android之光线传感器
  19. SQlite3基本用法,使用sublime编辑器
  20. js数组操作的一些方法在面试题的使用

热门文章

  1. 克隆虚拟机Centos7,更改各虚拟机IP地址,导致各虚拟机右上角网络标识不见
  2. java虚拟机手机下载_java虚拟机
  3. web微信开发工具,CSS浮动实战
  4. 关于Android 版本向下兼容
  5. 回溯算法模板之:332. 重新安排行程
  6. matlab 绘制误差阴影图
  7. 江山如此多娇_拔剑-浆糊的传说_新浪博客
  8. 基于网络爬虫的商品询价系统的设计与实现
  9. linux53端口,Linux的DNS的53端口
  10. [从头学数学] 第124节 不等式与不等式组