dom4j iterator方式解析xml文件

本文详细介绍了使用Dom4j库解析XML数据并运用XPath表达式进行查询的方法,通过实例展示了如何从XML响应中提取特定信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

dom4j下载地址:http://sourceforge.net/projects/dom4j/

 

SAXReader saxreader = new SAXReader();

		Document document = saxreader
				.read(new URL(
						"http://www.twototwo.cn/train/Service.aspx?format=xml&action=QueryTrainScheduleByTwoStation&key=80e12502-f08a-406b-b4c3-a682c2668937&startStation=%e5%8c%97%e4%ba%ac&arriveStation=%e4%b8%8a%e6%b5%b7&startDate=2013-03-15&ignoreStartDate=0&like=1&pageIndex=1&pageSize=2&more=0"));

		Element root = document.getRootElement();
		// System.out.println(root.getText());
		System.out.println(root.getName());
		Element main=(Element) root.elementIterator("Main").next();
//		Element item=(Element) main.elementIterator("Item").next();
		for (Iterator<Element> it=main.elementIterator("Item");it.hasNext();) {
			for (Iterator<Element> it1=it.next().elementIterator("CheCiMingCheng");it1.hasNext();) {
				System.out.println(it1.next().getText());
			}
		}


 使用    XML Path Language (XPath) 语句  http://www.w3.org/TR/xpath/

 

//		List<Element> le=document.selectNodes("/Response/Main/Item");
		List<Attribute> le=document.selectNodes("//Item/@Id");   //   //所有的含有id属性的item中的无素   //跳过父元素
		for (Attribute element : le) {
			System.out.println(element.getValue());    //@属性
		}
//		List<Element> le=document.selectNodes("//Item");   //   //所有的item中的无素   //跳过父元素
//		for (Element element : le) {
//			System.out.println(element.valueOf("@Id"));    //@属性
//		}


 

 

public class TrainUtilsDom4jIterator {

	/**
	 * 需要 jaxen-1.1-beta-7.jar
	 * 
	 * @param args
	 * @throws DocumentException
	 * @throws MalformedURLException
	 */
	public static void main(String[] args) throws MalformedURLException,
			DocumentException {
		// TODO Auto-generated method stub
		SAXReader saxreader = new SAXReader();

		Document document = saxreader
				.read(new URL(
						"http://www.twototwo.cn/train/Service.aspx?format=xml&action=QueryTrainScheduleByTwoStation&key=80e12502-f08a-406b-b4c3-a682c2668937&startStation=%e5%8c%97%e4%ba%ac&arriveStation=%e4%b8%8a%e6%b5%b7&startDate=2013-03-15&ignoreStartDate=0&like=1&pageIndex=1&pageSize=2&more=0"));
		//直接到此节点下
		List<Element> le=document.selectNodes("/Response/Main/Item");
		for (Iterator iterator = le.iterator(); iterator.hasNext();) {
			Element element = (Element) iterator.next();
			for(Iterator<Element> iterator1=element.elementIterator();iterator1.hasNext();){
				element =iterator1.next();
				System.out.println(element.getName()+" : "+element.getText());
			}
		}


 


url所返回的数据 为火车票查询接口返回数据

<?xml version="1.0" encoding="UTF-8"?>
<Response>
	<Head>
		<ChuFaZhan>
			<CheZhanBianHao>3ea57ec8-15ce-4574-b341-8e06e0c747b0</CheZhanBianHao>
			<CheZhanBianMa>1051033288</CheZhanBianMa>
			<CheZhanMingCheng>北京</CheZhanMingCheng>
		</ChuFaZhan>
		<DaoDaZhan>
			<CheZhanBianHao>b7b95a33-cffd-433f-97d1-7d58c66b2d75</CheZhanBianHao>
			<CheZhanBianMa>1212589517</CheZhanBianMa>
			<CheZhanMingCheng>上海</CheZhanMingCheng>
		</DaoDaZhan>
	</Head>
	<Main Rows="2">
		<Item Id="1">
			<CheCiBianHao>9452769f-3de2-4b6d-a962-e0efeb990c9c</CheCiBianHao>
			<CheCiBianMa>1806535009</CheCiBianMa>
			<CheCiMingCheng>G101</CheCiMingCheng>
			<CheXingMingCheng>高速动车</CheXingMingCheng>
			<ShiFaZhan>北京南</ShiFaZhan>
			<ChuFaZhan>北京南</ChuFaZhan>
			<DaoDaZhan>上海虹桥</DaoDaZhan>
			<ZhongDianZhan>上海虹桥</ZhongDianZhan>
			<FaShi>07:00</FaShi>
			<DaoShi>12:23</DaoShi>
			<LiShi>05:23</LiShi>
			<LiCheng>1318</LiCheng>
			<ShangWuZuo>1748</ShangWuZuo>
			<TeDengZuo>--</TeDengZuo>
			<YiDengZuo>933</YiDengZuo>
			<ErDengZuo>553</ErDengZuo>
			<GaoJiRuanWoShang>--</GaoJiRuanWoShang>
			<GaoJiRuanWoXia>--</GaoJiRuanWoXia>
			<RuanWoShang>--</RuanWoShang>
			<RuanWoXia>--</RuanWoXia>
			<YingWoShang>--</YingWoShang>
			<YingWoZhong>--</YingWoZhong>
			<YingWoXia>--</YingWoXia>
			<RuanZuo>--</RuanZuo>
			<YingZuo>--</YingZuo>
		</Item>
		<Item Id="2">
			<CheCiBianHao>7455f9b1-f8d4-4dd1-abac-bf3235704b2a</CheCiBianHao>
			<CheCiBianMa>1951791537</CheCiBianMa>
			<CheCiMingCheng>G103</CheCiMingCheng>
			<CheXingMingCheng>高速动车</CheXingMingCheng>
			<ShiFaZhan>北京南</ShiFaZhan>
			<ChuFaZhan>北京南</ChuFaZhan>
			<DaoDaZhan>上海虹桥</DaoDaZhan>
			<ZhongDianZhan>上海虹桥</ZhongDianZhan>
			<FaShi>07:05</FaShi>
			<DaoShi>12:30</DaoShi>
			<LiShi>05:25</LiShi>
			<LiCheng>1318</LiCheng>
			<ShangWuZuo>1748</ShangWuZuo>
			<TeDengZuo>1053</TeDengZuo>
			<YiDengZuo>933</YiDengZuo>
			<ErDengZuo>553</ErDengZuo>
			<GaoJiRuanWoShang>--</GaoJiRuanWoShang>
			<GaoJiRuanWoXia>--</GaoJiRuanWoXia>
			<RuanWoShang>--</RuanWoShang>
			<RuanWoXia>--</RuanWoXia>
			<YingWoShang>--</YingWoShang>
			<YingWoZhong>--</YingWoZhong>
			<YingWoXia>--</YingWoXia>
			<RuanZuo>--</RuanZuo>
			<YingZuo>--</YingZuo>
		</Item>
	</Main>
	<Page>
		<PageIndex>1</PageIndex>
		<PageSize>2</PageSize>
		<TotalRecord>56</TotalRecord>
		<TotalPage>28</TotalPage>
	</Page>
	<Foot>
		<Time>24.05</Time>
		<Site>http://www.twototwo.cn</Site>
		<Json>{"format":"xml","action":"QueryTrainScheduleByTwoStation","key":"80e12502-f08a-406b-b4c3-a682c2668937","startStation":"%e5%8c%97%e4%ba%ac","arriveStation":"%e4%b8%8a%e6%b5%b7","startDate":"2013-03-15","ignoreStartDate":"0","like":"1","pageIndex":"1","pageSize":"2","more":"0"}
		</Json>
		<Info>{"format":"xml","action":"QueryTrainScheduleByTwoStation","key":"80e12502-f08a-406b-b4c3-a682c2668937","startStation":"北京","arriveStation":"上海","startDate":"2013-03-15","ignoreStartDate":"0","like":"1","pageIndex":"1","pageSize":"2","more":"0"}
		</Info>
	</Foot>
</Response>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值