Java获取新浪天气预报代码
2017-07-31 15:43
423 查看
原文地址:http://blog.csdn.net/huiwenjie168/article/details/27332779
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>();
// 初始化每个节点的值为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.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 block
e1.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();
}
}
}
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>();
// 初始化每个节点的值为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.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 block
e1.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获取新浪天气预报代码
- java获取新浪天气预报代码
- JAVA调用新浪天气预报代码(返回xml格式)
- 正则表达式应用笔记----解析网页,获取并显示“未来三天全国天气预报”(java)
- 新浪天气预报代码及城市代码
- Java获取yahoo天气预报
- 根据新浪天气API获取各地天气状况(Java实现)
- Java获取Google天气预报 ---解决 输出乱码问题
- 天气预报 获取日出日落的代码
- Android编程实现获取新浪天气预报数据的方法
- java 实现天气预报 用的是新浪的接口
- java调用webservice天气预报(SOAP请求的方式获取天气信息并解析返回的XML)
- java 获取新浪天气样例
- js获取新浪天气接口的实现代码
- 根据新浪天气API获取各地天气状况(Java实现)
- Java获取yahoo天气预报
- Android 获取新浪天气预报数据
- 天气预报 正则获取指数信息 group(0)指数 group(1)概要 group(2)详细指数 代码
- 根据新浪天气API获取各地天气状况(Java实现)
- java 利用新浪天气API获取天气预报