利用dom4j解析xml--天气预报

       第一次解析xml,有点小激动,正好找个天气预报例子,放眼望去,不知道咋解析,但告诉自己如果这个经典例子都会了,以后一般的xml难不倒你.

后来就努力搜索相关资料,终于把我感兴趣的数据都解析出来了,在此跟大家一起分享下.

       在解析xml之前我们先获取下xml文件,这里以天气预报接口为例,

在浏览器里输入http://wthrcdn.etouch.cn/WeatherApi?city=杭州就可以获取到杭州的当天天气.

将获取到的网页保存为xml文件.网页部分内容为:

利用dom4j解析xml--天气预报_第1张图片


我将所有的节点收缩起来查看


主要有环境,昨天预报,未来天气预报,生活指数等.这里我们以解析未来天气预报和生活指数为例.

利用dom4j解析xml--天气预报_第2张图片

利用dom4j解析xml--天气预报_第3张图片利用dom4j解析xml--天气预报_第4张图片

首先我们需要根据xml创建Resp类.

public class Resp {

	public String city;
	public String updatetime;
	public String wendu;
	public String fengli;
	public String shidu;
	public String fengxiang;
	public String sunrise_1;
	public String sunset_1;
	public String sunrise_2;
	public String sunset_2;
	public List<Weather> listWeather;
	public List<Zhishu> listZhishu;
	
	
	public String getCity() {
		return city;
	}
	public void setCity(String city) {
		this.city = city;
	}
	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 getFengxiang() {
		return fengxiang;
	}
	public void setFengxiang(String fengxiang) {
		this.fengxiang = fengxiang;
	}
	public String getSunrise_1() {
		return sunrise_1;
	}
	public void setSunrise_1(String sunrise_1) {
		this.sunrise_1 = sunrise_1;
	}
	public String getSunset_1() {
		return sunset_1;
	}
	public void setSunset_1(String sunset_1) {
		this.sunset_1 = sunset_1;
	}
	public String getSunrise_2() {
		return sunrise_2;
	}
	public void setSunrise_2(String sunrise_2) {
		this.sunrise_2 = sunrise_2;
	}
	public String getSunset_2() {
		return sunset_2;
	}
	public void setSunset_2(String sunset_2) {
		this.sunset_2 = sunset_2;
	}
	public List<Weather> getListWeather() {
		return listWeather;
	}
	public void setListWeather(List<Weather> listWeather) {
		this.listWeather = listWeather;
	}
	public List<Zhishu> getListZhishu() {
		return listZhishu;
	}
	public void setListZhishu(List<Zhishu> listZhishu) {
		this.listZhishu = listZhishu;
	}
	
	
}

再创建其他类

package weather;

public class Weather {
	public String date;
	public String high;
	public String low;
	public String getDate() {
		return date;
	}
	public void setDate(String date) {
		this.date = date;
	}
	public String getHigh() {
		return high;
	}
	public void setHigh(String high) {
		this.high = high;
	}
	public String getLow() {
		return low;
	}
	public void setLow(String low) {
		this.low = low;
	}
	public Day getDay() {
		return day;
	}
	public void setDay(Day day) {
		this.day = day;
	}
	public Night getNight() {
		return night;
	}
	public void setNight(Night night) {
		this.night = night;
	}
	public Day day;
	public Night night;
}

package weather;

public class Day {
	
	public String type;
	public String fengxiang;
	public String fengli;
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getFengxiang() {
		return fengxiang;
	}
	public void setFengxiang(String fengxiang) {
		this.fengxiang = fengxiang;
	}
	public String getFengli() {
		return fengli;
	}
	public void setFengli(String fengli) {
		this.fengli = fengli;
	}

}

package weather;

public class Night {

	public String type;
	public String fengli;
	public String fengxiang;
	
	public Night(String type, String fengli, String fengxiang) {
		super();
		this.type = type;
		this.fengli = fengli;
		this.fengxiang = fengxiang;
	}
	public String getType() {
		return type;
	}
	public String getFengli() {
		return fengli;
	}
	public String getFengxiang() {
		return fengxiang;
	}
}

package weather;

public class Zhishu {
	public String name;
	public String value;
	public String detail;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getValue() {
		return value;
	}
	public void setValue(String value) {
		this.value = value;
	}
	public String getDetail() {
		return detail;
	}
	public void setDetail(String detail) {
		this.detail = detail;
	}
	
	
}

接下来我们就要编写解析XML类了.

package weather;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class XmlReader {
	public static final String fileName = "xml/weather.xml";

	public static Resp read(String fileName) throws DocumentException {
		File xmlFile = new File(fileName);
		return read(xmlFile);
	}

	private static Resp read(File xmlFile) throws DocumentException {

		// 使用SAX方式解析xml
		SAXReader saxReader = new SAXReader();
		// 把xml文件转换成文档对象
		Document document = saxReader.read(xmlFile);
		// 取得根节点
		Element rootNode = document.getRootElement();
		// 取得所有的二级(下一级)子节点
		return getNode(rootNode);
	}

	private static Resp getNode(Element rootNode) {
		// 新建Resp对象
		Resp resp = new Resp();
		// 获取城市名称
		resp.setCity(rootNode.element("city").getText());
		resp.setFengli(rootNode.element("fengli").getText());
		resp.setFengxiang(rootNode.element("fengxiang").getText());
		resp.setShidu(rootNode.element("shidu").getText());
		resp.setSunrise_1(rootNode.element("sunrise_1").getText());
		resp.setSunrise_2(rootNode.element("sunrise_2").getText());
		resp.setSunset_1(rootNode.element("sunset_1").getText());
		resp.setSunset_2(rootNode.element("sunset_2").getText());
		resp.setUpdatetime(rootNode.element("updatetime").getText());
		resp.setWendu(rootNode.element("wendu").getText());

		Element forecast = rootNode.element("forecast");
		List<Element> list = forecast.elements();
		//将weather集合设置进resp.
		resp.setListWeather(getWeather(list));
		
		Element zhishus = rootNode.element("zhishus");
		List<Element> listzhishus = zhishus.elements();
		//将zhishus集合设置进resp.
		resp.setListZhishu(getZhishus(listzhishus));
		
		return resp;
	}
	
	private static List getZhishus(List<Element> list) {
		List<Zhishu> zs_list = new ArrayList<Zhishu>();
		for (Element ele : list) {
			Zhishu zs = new Zhishu();
			// 获取每一天的天气
			zs.setName(ele.element("name").getText());
			// 获取当天的最高温度
			zs.setValue(ele.element("value").getText());
			// 获取当天的最低温度
			zs.setDetail(ele.element("detail").getText());
			
			zs_list.add(zs);
		}
		return zs_list;
	}

	private static List getWeather(List<Element> list) {
		List<Weather> wea_list = new ArrayList<Weather>();
		for (Element ele : list) {
			Weather weath = new Weather();
			// 获取每一天的天气
			weath.setDate(ele.element("date").getText());
			// 获取当天的最高温度
			weath.setHigh(ele.element("high").getText());
			// 获取当天的最低温度
			weath.setLow(ele.element("low").getText());
			// 获取day
			if (ele.element("day")!=null) {
				Element day_ele = ele.element("day");
				Day day = new Day();
				day.setType(day_ele.element("type").getText());
				day.setFengli(day_ele.element("fengli").getText());
				day.setFengxiang(day_ele.element("fengxiang").getText());
				weath.setDay(day);
			}
			//获取night
			if (ele.element("night")!=null) {
				Element day_ele = ele.element("night");
				Night night = new Night(day_ele.element(
										"type").getText(), day_ele.element("fengli").getText(),
										day_ele.element("fengxiang").getText());
				weath.setNight(night);
			}
			wea_list.add(weath);
		}
		return wea_list;
	}

	//测试
	public static void main(String[] args) throws DocumentException {
		Resp resp = read(fileName);
		System.out.println(resp.getCity());
		System.out.println("湿度 "+resp.getShidu());
		List<Weather> list = resp.getListWeather();
		List<Zhishu> zslist = resp.getListZhishu();
		
		System.out.println("***************************************************");
		for(Weather wea : list){			
			System.out.println(wea.getDate());
			System.out.println(wea.getHigh());
			System.out.println(wea.getDay().getType());
			System.out.println(wea.getDay().getFengxiang());	
			System.out.println("--------------------------------------");
		}
		System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
		for(Zhishu zs : zslist){			
			System.out.println(zs.getName());
			System.out.println(zs.getValue());
			System.out.println(zs.getDetail());
			System.out.println("--------------------------------------");
		}
	}

}


当然了我们还得在lib目录下添加dom4j-1.6.1.jar

到此为止我们已经将我们感兴趣的内容提取到Resp里了,赶紧动手起来运行看看吧~


你可能感兴趣的:(xml,dom)