java解析xml实例——获取天气信息
生活随笔
收集整理的這篇文章主要介紹了
java解析xml实例——获取天气信息
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
獲取xml并解析其中的數(shù)據(jù):
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 { /*** 獲取網(wǎng)頁中的xml* @param url 需要傳入的網(wǎng)址* @return 返回的xml數(shù)據(jù)內(nèi)容,數(shù)據(jù)格式為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 blocke.printStackTrace();}return xmlContent;}/*** 獲取天氣信息數(shù)據(jù)集合* @param document 獲取到的xml數(shù)據(jù)內(nèi)容* @param dataTagNames 需要從xml中獲取的數(shù)據(jù)對應的TagName * @return 返回包含天氣數(shù)據(jù)內(nèi)容的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; }/*** 獲取天氣信息數(shù)據(jù)集合(主要用于獲取yesterday的天氣信息)* @param document 獲取到的xml數(shù)據(jù)內(nèi)容* @param fatherTagName 需要從xml中獲取的數(shù)據(jù)對應的TagName的父級標簽的TagName * @param dataTagNames 需要從xml中獲取的數(shù)據(jù)對應的TagName * @return 返回包含天氣數(shù)據(jù)內(nèi)容的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; }/*** 獲取天氣信息數(shù)據(jù)集合(主要用于獲取yesterday的天氣信息)* @param document 獲取到的xml數(shù)據(jù)內(nèi)容* @param grandFatherTagName 需要從xml中獲取的數(shù)據(jù)對應的TagName的祖父級標簽的TagName * @param fatherTagName 需要從xml中獲取的數(shù)據(jù)對應的TagName的父級標簽的TagName * @param dataTagNames 需要從xml中獲取的數(shù)據(jù)對應的TagName * @return 返回包含天氣數(shù)據(jù)內(nèi)容的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; //數(shù)據(jù)更新時間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; //空氣質(zhì)量描述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;}@Overridepublic String toString() {return "WeatherBean [date=" + date + ", hightTemp=" + hightTemp + ", lowTemp=" + lowTemp + ", dayWeatherDes="+ dayWeatherDes + ", dayWindDirection=" + dayWindDirection + ", dayWindType=" + dayWindType+ ", nightWeatherDes=" + nightWeatherDes + ", nightWindDirection=" + nightWindDirection+ ", nightWindType=" + nightWindType + "]";}}
?
轉(zhuǎn)載于:https://www.cnblogs.com/caoleiCoding/p/7368069.html
總結(jié)
以上是生活随笔為你收集整理的java解析xml实例——获取天气信息的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FirstDjangoWebApp-1
- 下一篇: 进程句柄和进程ID的区别和关系