被解析的XML文件
<?xml version="1.0" encoding="UTF-8"?>
<resp>
<city>北京</city>
<updatetime>19:32</updatetime>
<wendu>18</wendu>
<fengli><![CDATA[1级]]></fengli>
<shidu>36%</shidu>
<fengxiang>西南风</fengxiang>
<sunrise_1>05:14</sunrise_1>
<sunset_1>19:04</sunset_1>
<yesterday>
<date_1>6日星期一</date_1>
<high_1>高温 22℃</high_1>
<low_1>低温 8℃</low_1>
<day_1>
<type_1>多云</type_1>
<fx_1>北风</fx_1>
<fl_1><![CDATA[3-4级]]></fl_1>
</day_1>
<night_1>
<type_1>晴</type_1>
<fx_1>北风</fx_1>
<fl_1><![CDATA[3-4级]]></fl_1>
</night_1>
</yesterday>
<forecast>
<weather>
<date>7日星期二</date>
<high>高温 23℃</high>
<low>低温 12℃</low>
<day>
<type>晴</type>
<fengxiang>南风</fengxiang>
<fengli><![CDATA[3-4级]]></fengli>
</day>
<night>
<type>多云</type>
<fengxiang>南风</fengxiang>
<fengli><![CDATA[3-4级]]></fengli>
</night>
</weather>
<weather>
<date>8日星期三</date>
<high>高温 22℃</high>
<low>低温 11℃</low>
<day>
<type>多云</type>
<fengxiang>南风</fengxiang>
<fengli><![CDATA[3-4级]]></fengli>
</day>
<night>
<type>多云</type>
<fengxiang>南风</fengxiang>
<fengli><![CDATA[3-4级]]></fengli>
</night>
</weather>
<weather>
<date>9日星期四</date>
<high>高温 26℃</high>
<low>低温 13℃</low>
<day>
<type>晴</type>
<fengxiang>南风</fengxiang>
<fengli><![CDATA[3-4级]]></fengli>
</day>
<night>
<type>多云</type>
<fengxiang>南风</fengxiang>
<fengli><![CDATA[3-4级]]></fengli>
</night>
</weather>
<weather>
<date>10日星期五</date>
<high>高温 28℃</high>
<low>低温 15℃</low>
<day>
<type>多云</type>
<fengxiang>南风</fengxiang>
<fengli><![CDATA[3-4级]]></fengli>
</day>
<night>
<type>晴</type>
<fengxiang>南风</fengxiang>
<fengli><![CDATA[3-4级]]></fengli>
</night>
</weather>
<weather>
<date>11日星期六</date>
<high>高温 29℃</high>
<low>低温 16℃</low>
<day>
<type>多云</type>
<fengxiang>南风</fengxiang>
<fengli><![CDATA[3-4级]]></fengli>
</day>
<night>
<type>阴</type>
<fengxiang>南风</fengxiang>
<fengli><![CDATA[3-4级]]></fengli>
</night>
</weather>
</forecast>
<zhishus>
<zhishu>
<name>晨练指数</name>
<value>较适宜</value>
<detail>早晨气象条件较适宜晨练,但风力稍大,晨练时请注意选择避风的地点,避免迎风锻炼。</detail>
</zhishu>
<zhishu>
<name>舒适度</name>
<value>舒适</value>
<detail>白天不太热也不太冷,风力不大,相信您在这样的天气条件下,应会感到比较清爽和舒适。</detail>
</zhishu>
<zhishu>
<name>穿衣指数</name>
<value>较舒适</value>
<detail>建议着薄外套、开衫牛仔衫裤等服装。年老体弱者应适当添加衣物,宜着夹克衫、薄毛衣等。</detail>
</zhishu>
<zhishu>
<name>感冒指数</name>
<value>较易发</value>
<detail>天凉,昼夜温差较大,较易发生感冒,请适当增减衣服,体质较弱的朋友请注意适当防护。</detail>
</zhishu>
<zhishu>
<name>晾晒指数</name>
<value>适宜</value>
<detail>天气不错,适宜晾晒。赶紧把久未见阳光的衣物搬出来吸收一下太阳的味道吧!</detail>
</zhishu>
<zhishu>
<name>旅游指数</name>
<value>适宜</value>
<detail>天气较好,温度适宜,但风稍微有点大。这样的天气适宜旅游,您可以尽情地享受大自然的无限风光。</detail>
</zhishu>
<zhishu>
<name>紫外线强度</name>
<value>弱</value>
<detail>紫外线强度较弱,建议出门前涂擦SPF在12-15之间、PA+的防晒护肤品。</detail>
</zhishu>
<zhishu>
<name>洗车指数</name>
<value>较适宜</value>
<detail>较适宜洗车,未来一天无雨,风力较小,擦洗一新的汽车至少能保持一天。</detail>
</zhishu>
<zhishu>
<name>运动指数</name>
<value>较适宜</value>
<detail>天气较好,但因风力稍强,户外可选择对风力要求不高的运动,推荐您进行室内运动。</detail>
</zhishu>
<zhishu>
<name>约会指数</name>
<value>较适宜</value>
<detail>虽然有点风,但情侣们可以放心外出,不用担心天气来调皮捣乱而影响了兴致。</detail>
</zhishu>
<zhishu>
<name>雨伞指数</name>
<value>不带伞</value>
<detail>天气较好,不会降水,因此您可放心出门,无须带雨伞。</detail>
</zhishu>
</zhishus>
</resp>
解析类
package com.leo;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
public class Config {
private static List<String> doc = new ArrayList<>();
private static Document parse(InputStream inputStream) throws DocumentException {
return new SAXReader().read(inputStream);
}
private static void bar(Document document) {
parseDoc(document.getRootElement());
}
private static void parseDoc(Element node) {
doc.add(node.getName() + "->" + node.getTextTrim());
List<Element> elements = node.elements();
for (Element e : elements) {
parseDoc(e);
}
}
public static List<String> getDoc() throws DocumentException {
bar(parse(Config.class.getResourceAsStream("/WeatherApi.xml")));
return doc;
}
}
解析结果