java 新浪天气预报接口_根据新浪天气API获取各地天气状况(Java实现)
1.代码块package com.quickmanager.util;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.FileInputStream;
import java.net.MalformedURLException;
import java.net.URL;
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
* @author cyxl
* @version 1.0 2012-05-24
* @since 1.0
*
*/
public class XmlParser {
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 XmlParser() {
}
public XmlParser(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 XmlParser(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 XmlParser(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 getValue(String[] nodes) {
if (inStream == null || root==null) {
return null;
}
Map map = new HashMap();
// 初始化每个节点的值为null
for (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.getFirstChild().getNodeValue();
String val = node.getTextContent();
System.out.println(nodes[j] + ":" + val);
// 如果原来已经有值则以分号分隔
String temp = map.get(nodes[j]);
if (temp != null && !temp.equals("")) {
temp = temp + ";" + val;
} else {
temp = val;
}
map.put(nodes[j], temp);
}
}
}
}
}
}
}
return map;
}
}2.测试代码
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";
URL url;
String path = "test.xml";
try {
url = new URL(link);
System.out.println(url);
// InputStream inStream= url.openStream();
// InputStream inStream=new FileInputStream(new File("test.xml"));
XmlParser parser = new XmlParser(url);
String[] nodes = {"status1","temperature1","temperature2"};
Map map = parser.getValue(nodes);
System.out.println(map.get(nodes[0]));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}3.结果
http://php.weather.sina.com.cn/xml.php?city=%D6%D8%C7%EC&password=DJOYnieT8234jlsK&day=0
status1:阵雨
temperature1:21
temperature2:18
阵雨
转载学习作品,如若得罪版主。sorry。
有需要的话以下有原链接!
java 新浪天气预报接口_根据新浪天气API获取各地天气状况(Java实现)相关推荐
- java调银企互联接口_银企互联NC中间件(演示:.NET、Java、Php、Python)
*本程序非官方,作为企业客户的财务系统或客户端的一部分,由企业客户做好安全保护和维护* * 使用本程序,视作已知晓且同意* Java.Php.Python我都没学过,网上摘了点代码,自己修改调试,完成 ...
- java 朋友圈分享接口_微信发朋友圈api接口调用代码
微信发朋友圈api接口调用代码,推送微信朋友圈.发朋友圈 /** * 触发推送朋友圈列表 * @author wechatno:tangjinjinwx * startTime传秒 * @blog h ...
- java 周易解梦接口_周公解梦-免费API,收集所有免费的API
/** * Created by PhpStorm. * User: FZS * Time: 2019/3/15 17:50 */ //-------------------------------- ...
- 2021-06-19复习java Collection集合 Iterator接口_迭代器 增强for循环 泛型
2021-06-19复习java Collection集合 Iterator接口_迭代器 增强for循环 泛型 Collection集合 java.util.coLlection接口 所有单列集合的最 ...
- 若依前后端分离版:增加新的登录接口,用于小程序或者APP获取token,并使用若依的验证方法
LoginController类 具体代码 /*** app 登录*/@AnonymousAccess@PostMapping("login")public AjaxResult ...
- java 利用新浪天气API获取天气预报
新浪为我们提供了天气预报获取接口API http://php.weather.sina.com.cn/xml.php?city=武汉&password=DJOYnieT8234jlsK&am ...
- 新浪股票接口 php,通过新浪股票行情接口,怎么判断当天除权除息的股票?
我想制作一个功能,点下按钮,就可以把当天除权除息的股票列出来,我的网站股票行情是调用的新浪接口. 不知道谁有开发过这个功能的,或者有技术开发这个小功能的,本人不甚感激,也可有酬劳略表心意哦. 回复讨论 ...
- 学习笔记之-java8的新特性-函数式接口,lambda表达式,方法引用,Stream API,Optional类
1.Lambda表达式 用匿名内部类的方法去创建多线程1.new Thread2.参数传递new Runnable3.重写run方法4.在run方法中去设置线程任务5.调用start问题:我们最终目标 ...
- java异常的总接口_重构:Java特别的接口修改:在throws子句中添加一个异常?
我用具体的代码来解释下吧 接口A,有方法X public interface A { public void methodX() throws IOException; } 在用户模块有这样一段调用 ...
最新文章
- C#开发微信门户及应用(7)-微信多客服功能及开发集成
- shell 下的运算表达
- KMP算法的动态规划解说
- 用 Visual Studio Code 在 macOS 上创建首个 ASP.NET Core 应用程序
- 34个省市自治区排序_freeCodeCamp的1,000多个学习小组现已完全自治
- STM32一种基于NTC的控温电路及软件实现
- 前端基础之CSS(总结)
- WP7 初始屏幕设置
- java应用servlt_关于Java:将Servlet用于非Servlet应用程序
- #化鲲为鹏,我有话说# 鲲鹏云服务器的使用小技巧之SSH(一)
- 局部变量与全局变量同名时如何在局部变量的作用范围内访问全局变量?
- Windows Performance Monitor 学习笔记
- 借助Excel批量重命名图片、文档,以及处理文件名中的空格问题(适合新手小白)
- 安阳工学院计算机类宿舍,2021年安阳工学院新生宿舍条件和宿舍环境图片
- p3c 插件,是怎么检查出你那屎山的代码?
- XXljob 使用教程(springboot)
- python广播机制是什么意思_Python numpy 广播机制
- [USACO09MAR]向右看齐Look Up————单调栈
- mysql查询男女平均年龄_查询计算机系学生的姓名、性别和年龄
- 思科胖AP多个SSID配置实例