java解析xml实例——获取天气信息
获取xml并解析其中的数据:
package getweather.xml; import java.io.IOException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements; import getweather.beans.TodayWeather;
import getweather.beans.WeatherBean; public class GetXml {
/**
* 获取网页中的xml
* @param url 需要传入的网址
* @return 返回的xml数据内容,数据格式为org.jsoup.nodes.Document
*/
public Document getXml(String url){ Document xmlContent = null;
try {
if((url!=null)||(("").equals(url))){
xmlContent = Jsoup.connect(url)
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(3000)
.post();
} } catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return xmlContent;
} /**
* 获取天气信息数据集合
* @param document 获取到的xml数据内容
* @param dataTagNames 需要从xml中获取的数据对应的TagName
* @return 返回包含天气数据内容的Map<String,String>集合
*/ public Map<String, String> getData(Document document,String dataTagNames[]){
Map<String, String> weatherMap = new HashMap<String,String>();
if(document!=null){
for (String dataTagName : dataTagNames) {
Elements elementsData = document.getElementsByTag(dataTagName);
String data = elementsData.first().text();
weatherMap.put(dataTagName, data);
} } return weatherMap;
} /**
* 获取天气信息数据集合(主要用于获取yesterday的天气信息)
* @param document 获取到的xml数据内容
* @param fatherTagName 需要从xml中获取的数据对应的TagName的父级标签的TagName
* @param dataTagNames 需要从xml中获取的数据对应的TagName
* @return 返回包含天气数据内容的Map<String,String>集合
*/ public Map<String, String> getData(Document document,String fatherTagName,String dataTagNames[]){
Map<String, String> weatherMap = new HashMap<String,String>();
if(document!=null){
Element fatherElement = document.getElementsByTag(fatherTagName).first();
Elements elementsData;
for (String dataTagName : dataTagNames) { elementsData = fatherElement.getElementsByTag(dataTagName);
if(!(elementsData.isEmpty())){
for (Element element : elementsData) {
weatherMap.put(element.tagName(), element.text());
}
}
} } return weatherMap;
} /**
* 获取天气信息数据集合(主要用于获取yesterday的天气信息)
* @param document 获取到的xml数据内容
* @param grandFatherTagName 需要从xml中获取的数据对应的TagName的祖父级标签的TagName
* @param fatherTagName 需要从xml中获取的数据对应的TagName的父级标签的TagName
* @param dataTagNames 需要从xml中获取的数据对应的TagName
* @return 返回包含天气数据内容的Map<String,String>集合
*/ public Map<String, String> getData(Document document,String grandFatherTagName,String fatherTagName,String dataTagNames[]){
Map<String, String> weatherMap = new HashMap<String,String>();
if(document!=null){
Element fatherElement = (document.getElementsByTag(grandFatherTagName).first()).getElementsByTag(fatherTagName).first();
Element elementsData;
for (String dataTagName : dataTagNames) {
elementsData = fatherElement.getElementsByTag(dataTagName).first();
weatherMap.put(elementsData.tagName(), elementsData.text());
} } return weatherMap;
} public Elements getForecastWeather(Document document,String tagName){
Elements weatherElements = null;
if(document!=null){
weatherElements = document.getElementsByTag(tagName);
}
return weatherElements;
} public Map<String, String> getData(Element element,String dataTagNames[]){
Map<String, String> weatherMap = new HashMap<String,String>();
if(element!=null){
for (String dataTagName : dataTagNames) {
Elements elementsData = element.getElementsByTag(dataTagName);
String data = elementsData.first().text();
weatherMap.put(dataTagName, data);
} } return weatherMap;
} public Map<String, String> getData2(Element element,String sonTagName,String dataTagNames[]){
Map<String, String> weatherMap = new HashMap<String,String>();
Elements elementsData = null;
if(element!=null){
for (String dataTagName : dataTagNames) {
elementsData = element.getElementsByTag(sonTagName).first().getElementsByTag(dataTagName);
String data = elementsData.first().text();
weatherMap.put(dataTagName, data);
} } return weatherMap;
} public TodayWeather getTodayW(){
TodayWeather todayWeather = new TodayWeather();
GetXml getweather = new GetXml();
Document xml = getweather.getXml("http://wthrcdn.etouch.cn/WeatherApi?city=上海");
String todayWeatherTagName[] ={"city","updatetime","wendu","fengli","shidu","fengxiang",
"sunrise_1","sunset_1","aqi","pm25","suggest","quality","o3","co","pm10","so2",
"no2"};
Map<String, String> todayWeatherData = getweather.getData(xml,todayWeatherTagName);
todayWeather.setWenDu(todayWeatherData.get("wendu"));
todayWeather.setShiDu(todayWeatherData.get("shidu"));
todayWeather.setQuality(todayWeatherData.get("quality"));
todayWeather.setFengLi(todayWeatherData.get("fengli"));
todayWeather.setFengXiang(todayWeatherData.get("fengxiang"));
todayWeather.setSunrise(todayWeatherData.get("sunrise_1"));
todayWeather.setSunset(todayWeatherData.get("sunset_1"));
todayWeather.setAqi(todayWeatherData.get("aqi"));
todayWeather.setPm25(todayWeatherData.get("pm25"));
todayWeather.setO3(todayWeatherData.get("o3"));
todayWeather.setCo(todayWeatherData.get("co"));
todayWeather.setPm10(todayWeatherData.get("pm10"));
todayWeather.setSo2(todayWeatherData.get("so2"));
todayWeather.setNo2(todayWeatherData.get("no2"));
todayWeather.setSuggest(todayWeatherData.get("suggest"));
todayWeather.setUpdateTime(todayWeatherData.get("updatetime")); return todayWeather;
} public WeatherBean getYesterdayW(){
WeatherBean yWeatherBean = new WeatherBean();
GetXml getweather = new GetXml();
Document xml = getweather.getXml("http://wthrcdn.etouch.cn/WeatherApi?city=上海");
String yesterdayWeatherTagNameSon[] = {"date_1","high_1","low_1"};
String yesterdayWeatherTagNameGrandson[]={"type_1","fx_1","fl_1"};
Map<String, String> yesterdayWeatherData = getweather.getData(xml,"yesterday",yesterdayWeatherTagNameSon);
Map<String, String> yesterdayWeatherData2 = getweather.getData(xml,"yesterday","day_1",yesterdayWeatherTagNameGrandson);
Map<String, String> yesterdayWeatherData3 = getweather.getData(xml,"yesterday","night_1",yesterdayWeatherTagNameGrandson);
yWeatherBean.setDayWeatherDes(yesterdayWeatherData2.get("type_1"));
yWeatherBean.setDayWindDirection(yesterdayWeatherData2.get("fx_1"));
yWeatherBean.setDayWindType(yesterdayWeatherData2.get("fl_1"));
yWeatherBean.setDate(yesterdayWeatherData.get("date_1"));
yWeatherBean.setHightTemp(yesterdayWeatherData.get("high_1"));
yWeatherBean.setLowTemp(yesterdayWeatherData.get("low_1"));
yWeatherBean.setNightWeatherDes(yesterdayWeatherData3.get("type_1"));
yWeatherBean.setNightWindDirection(yesterdayWeatherData3.get("fx_1"));
yWeatherBean.setNightWindType(yesterdayWeatherData3.get("fl_1")); return yWeatherBean;
} public List<WeatherBean> getForecasterdayW(){
List<WeatherBean> fWeatherList = new LinkedList<>();
WeatherBean fWeatherBean = null;
GetXml getweather = new GetXml();
Document xml = getweather.getXml("http://wthrcdn.etouch.cn/WeatherApi?city=上海");
String forecastWeatherTagNameSon[] = {"date","high","low"};
String forecastWeatherTagNameGrandson[] = {"type","fengxiang","fengli"}; Elements weatherElements = getweather.getForecastWeather(xml,"weather");
Map<String, String> forecastWeatherData =new HashMap<>();
Map<String, String> forecastWeatherData2 =new HashMap<>();
Map<String, String> forecastWeatherData3 =new HashMap<>();
for (Element element :weatherElements ) {
fWeatherBean = new WeatherBean();
forecastWeatherData = getweather.getData(element, forecastWeatherTagNameSon);
forecastWeatherData2 = getweather.getData2(element,"day", forecastWeatherTagNameGrandson);
forecastWeatherData3 = getweather.getData2(element,"night",forecastWeatherTagNameGrandson);
fWeatherBean.setDayWeatherDes(forecastWeatherData2.get("type"));
fWeatherBean.setDayWindDirection(forecastWeatherData2.get("fengxiang"));
fWeatherBean.setDayWindType(forecastWeatherData2.get("fengli"));
fWeatherBean.setDate(forecastWeatherData.get("date"));
fWeatherBean.setHightTemp(forecastWeatherData.get("high"));
fWeatherBean.setLowTemp(forecastWeatherData.get("low"));
fWeatherBean.setNightWeatherDes(forecastWeatherData3.get("type"));
fWeatherBean.setNightWindDirection(forecastWeatherData3.get("fengxiang"));
fWeatherBean.setNightWindType(forecastWeatherData3.get("fengli"));
fWeatherList.add(fWeatherBean); } return fWeatherList;
} }
today天气Beans
package getweather.beans; public class TodayWeather { private String updateTime; //数据更新时间
private String wenDu; //当前温度
private String fengLi; //风力等级
private String fengXiang; //风力等级
private String shiDu; //空气相对湿度
private String sunrise; //风向
private String sunset; //日出时间
private String aqi; //日落时间
private String pm25; //pm2.5浓度
private String quality; //空气质量描述
private String o3; //臭氧浓度
private String co; //一氧化碳浓度
private String pm10; //pm10浓度
private String so2; //二氧化硫浓度
private String no2; //二氧化氮浓度
private String suggest; //建议 public TodayWeather() {
} public String getUpdateTime() {
return updateTime;
} public void setUpdateTime(String updateTime) {
this.updateTime = updateTime;
} public String getWenDu() {
return wenDu;
} public void setWenDu(String wenDu) {
this.wenDu = wenDu;
} public String getFengLi() {
return fengLi;
} public void setFengLi(String fengLi) {
this.fengLi = fengLi;
} public String getShiDu() {
return shiDu;
} public void setShiDu(String shiDu) {
this.shiDu = shiDu;
} public String getSunrise() {
return sunrise;
} public void setSunrise(String sunrise) {
this.sunrise = sunrise;
} public String getSunset() {
return sunset;
} public void setSunset(String sunset) {
this.sunset = sunset;
} public String getAqi() {
return aqi;
} public void setAqi(String aqi) {
this.aqi = aqi;
} public String getPm25() {
return pm25;
} public void setPm25(String pm25) {
this.pm25 = pm25;
} public String getQuality() {
return quality;
} public void setQuality(String quality) {
this.quality = quality;
} public String getO3() {
return o3;
} public void setO3(String o3) {
this.o3 = o3;
} public String getCo() {
return co;
} public void setCo(String co) {
this.co = co;
} public String getPm10() {
return pm10;
} public void setPm10(String pm10) {
this.pm10 = pm10;
} public String getSo2() {
return so2;
} public void setSo2(String so2) {
this.so2 = so2;
} public String getNo2() {
return no2;
} public void setNo2(String no2) {
this.no2 = no2;
} public String getSuggest() {
return suggest;
} public void setSuggest(String suggest) {
this.suggest = suggest;
} public String getFengXiang() {
return fengXiang;
} public void setFengXiang(String fengXiang) {
this.fengXiang = fengXiang;
} }
Yesterday天气及未来天气Beans
package getweather.beans; public class WeatherBean {
private String date;//日期
private String hightTemp;//最高温度
private String lowTemp;//最低温度
private String dayWeatherDes;//天气描述
private String dayWindDirection;//风向
private String dayWindType;//风力
private String nightWeatherDes;//夜间天气描述
private String nightWindDirection;//夜间风向
private String nightWindType;//夜间风力等级 public WeatherBean() {
} public String getDate() {
return date;
} public void setDate(String date) {
this.date = date;
} public String getHightTemp() {
return hightTemp;
} public void setHightTemp(String hightTemp) {
this.hightTemp = hightTemp;
} public String getLowTemp() {
return lowTemp;
} public void setLowTemp(String lowTemp) {
this.lowTemp = lowTemp;
} public String getDayWeatherDes() {
return dayWeatherDes;
} public void setDayWeatherDes(String dayWeatherDes) {
this.dayWeatherDes = dayWeatherDes;
} public String getDayWindDirection() {
return dayWindDirection;
} public void setDayWindDirection(String dayWindDirection) {
this.dayWindDirection = dayWindDirection;
} public String getDayWindType() {
return dayWindType;
} public void setDayWindType(String dayWindType) {
this.dayWindType = dayWindType;
} public String getNightWeatherDes() {
return nightWeatherDes;
} public void setNightWeatherDes(String nightWeatherDes) {
this.nightWeatherDes = nightWeatherDes;
} public String getNightWindDirection() {
return nightWindDirection;
} public void setNightWindDirection(String nightWindDirection) {
this.nightWindDirection = nightWindDirection;
} public String getNightWindType() {
return nightWindType;
} public void setNightWindType(String nightWindType) {
this.nightWindType = nightWindType;
} @Override
public String toString() {
return "WeatherBean [date=" + date + ", hightTemp=" + hightTemp + ", lowTemp=" + lowTemp + ", dayWeatherDes="
+ dayWeatherDes + ", dayWindDirection=" + dayWindDirection + ", dayWindType=" + dayWindType
+ ", nightWeatherDes=" + nightWeatherDes + ", nightWindDirection=" + nightWindDirection
+ ", nightWindType=" + nightWindType + "]";
} }
java解析xml实例——获取天气信息的更多相关文章
- Java通过webservice接口获取天气信息
通过SOAP请求的方式获取天气信息并解析返回的XML文件. 参考: http://www.webxml.com.cn/WebServices/WeatherWS.asmx import java.io ...
- java 解析 XML实例
package com.hseact.fecp.servlet; import java.io.IOException; import javax.xml.parsers.DocumentBuilde ...
- Java解析XML文档(简单实例)——dom解析xml
一.前言 用Java解析XML文档,最常用的有两种方法:使用基于事件的XML简单API(Simple API for XML)称为SAX和基于树和节点的文档对象模型(Document Object ...
- java获取天气信息
通过天气信息接口获取天气信息,首先要给项目导入程序所需要的包,具体需要如下几个包: json-lib-2.4.jar ezmorph-1.0.6.jar commons-beanutils-1.8.3 ...
- java解析xml的三种方法
java解析XML的三种方法 1.SAX事件解析 package com.wzh.sax; import org.xml.sax.Attributes; import org.xml.sax.SAXE ...
- Java 解析 XML
Java 解析 XML 标签: Java基础 XML解析技术有两种 DOM SAX DOM方式 根据XML的层级结构在内存中分配一个树形结构,把XML的标签,属性和文本等元素都封装成树的节点对象 优点 ...
- XML概念定义以及如何定义xml文件编写约束条件java解析xml DTD XML Schema JAXP java xml解析 dom4j 解析 xpath dom sax
本文主要涉及:xml概念描述,xml的约束文件,dtd,xsd文件的定义使用,如何在xml中引用xsd文件,如何使用java解析xml,解析xml方式dom sax,dom4j解析xml文件 XML来 ...
- Java解析XML:Jdom解析和SAX解析
今天看了Java解析XML的几种方法,记录一下 1.Jdom解析 (1)读取XML内容 private static void readXML() { // TODO Auto-generated m ...
- Java解析XML汇总(DOM/SAX/JDOM/DOM4j/XPath)
[目录] 一.[基础知识——扫盲] 二.[DOM.SAX.JDOM.DOM4j简单使用介绍] 三.[性能测试] 四.[对比] 五.[小插曲XPath] 六.[补充] 关键字:Java解析xml.解析x ...
随机推荐
- SESSION应用的一个场合
Session其实指的就是访问者从到达某个特定主页到离开为止的那段时间.每 一访问者都会单独获得一个Session.在Web应用程序中,当一个用户访问该应用 时,Session类型的变量可以供这个用户 ...
- django 链接地址匹配流程
前提: 代码结构 步骤一: 下面为某个网页的链接地址 <body> {% if latest_article_list %} <ul> {% for article in la ...
- RadioButton控件选中、取消
js: var flag = true; function chkRadio(id) { id.checked = flag; flag = !flag; } aspx.cs: this.rbtKey ...
- JS获取ListBox所有项
JS代码: var listbox = document.getElementById("<%=lbCustom.ClientID %>"); var values = ...
- Spring4面向切面AOP
AOP(Aspect Oriented Programming)面向切面编程,通过预编译方式和运行期动态代理实现程序功能的横向多模块统一控制的一种技术.AOP是OOP的补充,是spring框架中的一个 ...
- linux命令学习笔记(18):locate 命令
locate 让使用者可以很快速的搜寻档案系统内是否有指定的档案.其方法是先建立一个包括系统内所有档案名称及 路径的数据库,之后当寻找时就只需查询这个数据库,而不必实际深入档案系统之中了.在一般的 d ...
- 3.1 第一个场景 HelloWorldScene
HelloWorldScene.h #ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos ...
- freeMarker(十四)——XML处理指南之必要的XML处理
学习笔记,选自freeMarker中文文档,译自 Email: ddekany at users.sourceforge.net 1.基本内容 假设程序员在数据模型中放置了一个XML文档,就是名为 d ...
- 【Caffe】Ubuntu 安装 Caffe gpu版
安装环境:Ubuntu 16.04lts 64位, gcc5.4 gpu1050ti,cuda8.0,cudnn5.1.10 1. 安装依赖库 sudo apt-get install libprot ...
- openfire存储中文字符乱码解决办法
转载于: Xmpp问题总结:处理Openfire 中文乱码问题(2) openfire是一个非常不错的IM服务器,而且是纯Java实现,具有多个平台的版本,他的数据存储可以采用多种数据库,如MySQL ...