用到的jar包:
dom4j-2.0.2
jaxen-1.1.6
1.解析weather.xml
<ArrayOfString>
<string>河南 南阳</string>
<string>南阳</string>
<string>1428</string>
<string>2019/05/31 15:00:02</string>
<string>今日天气实况:气温:28℃;风向/风力:东南风 3级;湿度:40%</string>
<string>紫外线强度:弱。空气质量:中。</string>
<string>
紫外线指数:弱,辐射较弱,涂擦SPF12-15、PA+护肤品。 健臻·血糖指数:易波动,血糖易波动,注意监测。 穿衣指数:热,适合穿T恤、短薄外套等夏季服装。 洗车指数:较适宜,无雨且风力较小,易保持清洁度。 空气污染指数:中,易感人群应适当减少室外活动。
</string>
<string>5月31日 多云转阴</string>
<string>20℃/30℃</string>
<string>南风转西南风小于3级</string>
<string>1.gif</string>
<string>2.gif</string>
<string>6月1日 多云</string>
<string>19℃/33℃</string>
<string>西南风3-4级</string>
<string>1.gif</string>
<string>1.gif</string>
<string>6月2日 多云</string>
<string>21℃/37℃</string>
<string>西南风3-4级</string>
<string>1.gif</string>
<string>1.gif</string>
<string>6月3日 多云</string>
<string>23℃/38℃</string>
<string>西南风3-4级</string>
<string>1.gif</string>
<string>1.gif</string>
<string>6月4日 阴转多云</string>
<string>23℃/37℃</string>
<string>南风4-5级转3-4级</string>
<string>2.gif</string>
<string>1.gif</string>
</ArrayOfString>
源代码:
package com.oracle.core;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
public class WeatherParse
{
public static void main(String[] args) throws IOException, DocumentException
{
SAXReader saxReader=new SAXReader();
InputStream resourceAsStream = WeatherParse.class.getClassLoader().getResourceAsStream("weather.xml");
Document read = saxReader.read(resourceAsStream);
List<Node> selectNodes = read.selectNodes("//string");
for(Node node:selectNodes)
{
Element e=(Element)node;
System.out.println(e.getText());
}
}
}
解析结果:
2.解析ticket.xml:
<DataSet xmlns="http://WebXml.com.cn/">
<xs:schema xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="getStationAndTime">
<xs:element name="getStationAndTime" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="TimeTable">
<xs:complexType>
<xs:sequence>
<xs:element name="TrainCode" type="xs:string" minOccurs="0"/>
<xs:element name="FirstStation" type="xs:string" minOccurs="0"/>
<xs:element name="LastStation" type="xs:string" minOccurs="0"/>
<xs:element name="StartStation" type="xs:string" minOccurs="0"/>
<xs:element name="StartTime" type="xs:string" minOccurs="0"/>
<xs:element name="ArriveStation" type="xs:string" minOccurs="0"/>
<xs:element name="ArriveTime" type="xs:string" minOccurs="0"/>
<xs:element name="KM" type="xs:short" minOccurs="0"/>
<xs:element name="UseDate" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<getStationAndTime xmlns="">
<TimeTable diffgr:id="TimeTable1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<TrainCode>D296</TrainCode>
<FirstStation>福州</FirstStation>
<LastStation>郑州</LastStation>
<StartStation>武汉</StartStation>
<StartTime>17:35:00</StartTime>
<ArriveStation>郑州</ArriveStation>
<ArriveTime>21:14:00</ArriveTime>
<KM>0</KM>
<UseDate>03:39</UseDate>
</TimeTable>
<TimeTable diffgr:id="TimeTable2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
<TrainCode>G1271\G1274</TrainCode>
<FirstStation>武汉</FirstStation>
<LastStation>沈阳北</LastStation>
<StartStation>武汉</StartStation>
<StartTime>07:25:00</StartTime>
<ArriveStation>郑州东</ArriveStation>
<ArriveTime>09:39:00</ArriveTime>
<KM>0</KM>
<UseDate>02:14</UseDate>
</TimeTable>
<TimeTable diffgr:id="TimeTable3" msdata:rowOrder="2" diffgr:hasChanges="inserted">
<TrainCode>G1275\G1278</TrainCode>
<FirstStation>武汉</FirstStation>
<LastStation>哈尔滨西</LastStation>
<StartStation>武汉</StartStation>
<StartTime>08:55:00</StartTime>
<ArriveStation>郑州东</ArriveStation>
<ArriveTime>11:19:00</ArriveTime>
<KM>0</KM>
<UseDate>02:24</UseDate>
</TimeTable>
<TimeTable diffgr:id="TimeTable4" msdata:rowOrder="3" diffgr:hasChanges="inserted">
<TrainCode>G1290\G1291</TrainCode>
<FirstStation>武汉</FirstStation>
<LastStation>长春</LastStation>
<StartStation>武汉</StartStation>
<StartTime>08:03:00</StartTime>
<ArriveStation>郑州东</ArriveStation>
<ArriveTime>10:24:00</ArriveTime>
<KM>0</KM>
<UseDate>02:21</UseDate>
</TimeTable>
<TimeTable diffgr:id="TimeTable5" msdata:rowOrder="4" diffgr:hasChanges="inserted">
<TrainCode>G1532</TrainCode>
<FirstStation>贵阳北</FirstStation>
<LastStation>徐州东</LastStation>
<StartStation>武汉</StartStation>
<StartTime>17:39:00</StartTime>
<ArriveStation>郑州东</ArriveStation>
<ArriveTime>19:56:00</ArriveTime>
<KM>0</KM>
<UseDate>02:17</UseDate>
</TimeTable>
<TimeTable diffgr:id="TimeTable6" msdata:rowOrder="5" diffgr:hasChanges="inserted">
<TrainCode>G1534</TrainCode>
<FirstStation>贵阳北</FirstStation>
<LastStation>郑州东</LastStation>
<StartStation>武汉</StartStation>
<StartTime>20:50:00</StartTime>
<ArriveStation>郑州东</ArriveStation>
<ArriveTime>22:57:00</ArriveTime>
<KM>0</KM>
<UseDate>02:07</UseDate>
</TimeTable>
<TimeTable diffgr:id="TimeTable7" msdata:rowOrder="6" diffgr:hasChanges="inserted">
<TrainCode>G1536</TrainCode>
<FirstStation>玉溪</FirstStation>
<LastStation>郑州东</LastStation>
<StartStation>武汉</StartStation>
<StartTime>19:27:00</StartTime>
<ArriveStation>郑州东</ArriveStation>
<ArriveTime>21:40:00</ArriveTime>
<KM>0</KM>
<UseDate>02:13</UseDate>
</TimeTable>
<TimeTable diffgr:id="TimeTable8" msdata:rowOrder="7" diffgr:hasChanges="inserted">
<TrainCode>G1546\G1547</TrainCode>
<FirstStation>南宁东</FirstStation>
<LastStation>西安北</LastStation>
<StartStation>武汉</StartStation>
<StartTime>15:41:00</StartTime>
<ArriveStation>郑州</ArriveStation>
<ArriveTime>17:54:00</ArriveTime>
<KM>0</KM>
<UseDate>02:13</UseDate>
</TimeTable>
<Tim