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 + "]"; } }