dom4j iterator方式解析xml文件
2013-03-15 15:58
495 查看
dom4j下载地址:http://sourceforge.net/projects/dom4j/
使用 XML Path Language (XPath) 语句 http://www.w3.org/TR/xpath/
url所返回的数据 为火车票查询接口返回数据
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>
相关文章推荐
- java解析xml文件(三种方式-(dom ,jdom ,dom4j)
- DOM4J方式解析、创建XML文件(实例)
- 使用Dom4j解析xml文件---SAXReader方式
- java解析xml文件(三种方式-(dom ,jdom ,dom4j)
- xml文件的概述与应用场景 xml文件的组成部分&如何编写xml xml的两种解析方式的原理 Dom4J开源工具的使用
- JAVA解析XML文件(四)---DOM4J方式解析
- 用dom4j的方式解析xml文件
- java解析xml文件(三种方式-(dom ,jdom ,dom4j)
- 解析xml文件的三种方式(sax ,dom ,dom4j)
- Dom4j的一个小例子,用于解析xml文件的元素获取方式(转)
- 解析XML文件(二)----使用dom4j方式(二)
- java解析xml文件(三种方式-(dom ,jdom ,dom4j)
- DOM、SAX、JDOM、DOM4J四种方式解析xml文件
- java解析xml文件(三种方式-(dom ,jdom ,dom4j)
- 解析XML文件(二)----使用dom4j方式(三)
- Java解析XML文件的四种方式之DOM4J解析
- Java之DOM4J方式解析XML文件(学习笔记)
- DOM4J方式解析XML文件
- xml文件--DOM4J方式解析xml文件
- java解析xml文件(三种方式-(dom ,jdom ,dom4j)