DOM解析XML字符串

编程技术  /  houtizong 发布于 3年前   90
建好项目后导入dom4j-1.6.1.jar
UtilXml.java
package demo;import java.util.Iterator;import java.util.List;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.DocumentHelper;import org.dom4j.Element;public class UtilXml {public static void main(String[] args) {String v_strXML = "<scheme><normal><time><start>08:30</start><end>12:00</end></time><time><start>13:30</start><end>17:30</end></time></normal><weekend><saturday><rest>1</rest></saturday><sunday><rest>1</rest></sunday></weekend><special><workday><date>2009-02-27</date></workday><holiday><date>2009-04-22</date></holiday><holiday><date>2009-04-26</date></holiday><workday><date>2009-04-28</date><time><start>10:00</start><end>12:00</end></time></workday><holiday><date>2009-04-29</date></holiday><workday><date>2009-04-30</date><time><start>10:30</start><end>17:00</end></time></workday></special></scheme>";Document doc = null;try {doc = DocumentHelper.parseText(v_strXML);} catch (DocumentException e2) {// TODO 自动生成 catch 块e2.printStackTrace();}Element root = doc.getRootElement();// 指向根节点// normal解析Element normal = root.element("normal");try {List lstTime = normal.elements("time");// 所有的Item节点for (int i = 0; i < lstTime.size(); i++) {Element etime = (Element) lstTime.get(i);Element start = etime.element("start");Element end = etime.element("end");System.out.println("start.getTextTrim()=" + start.getTextTrim());System.out.println("end.getTextTrim()=" + end.getTextTrim());}// Iterator Itr = lstTime.iterator();// while (Itr.hasNext()) {// Element etime = (Element) Itr.next();// 一个Item节点// Element start = etime.element("start");// Element end = etime.element("end");// System.out.println(start.getTextTrim());// System.out.println(end.getTextTrim());// }} catch (Exception e) {e.printStackTrace();}// weekend解析Element weekend = root.element("weekend");try {// 周6List lstSaturday = weekend.elements("saturday");// 所有的Item节点Iterator Itr1 = lstSaturday.iterator();while (Itr1.hasNext()) {Element eSaturday = (Element) Itr1.next();// 一个Item节点Element rest = eSaturday.element("rest");System.out.println("周六休息" + rest.getTextTrim());}// 周日List lstSunday = weekend.elements("sunday");// 所有的Item节点Iterator Itr2 = lstSunday.iterator();while (Itr2.hasNext()) {Element eSunday = (Element) Itr2.next();// 一个Item节点Element rest = eSunday.element("rest");System.out.println("周日休息" + rest.getTextTrim());}} catch (Exception e) {e.printStackTrace();}// special解析Element special = root.element("special");try {// 特殊工作日workdayList lstWorkday = special.elements("workday");// 所有的Item节点Iterator Itr1 = lstWorkday.iterator();while (Itr1.hasNext()) {Element eWorkday = (Element) Itr1.next();// 一个Item节点Element date = eWorkday.element("date");System.out.println("特殊工作日===" + date.getText());List time = eWorkday.elements("time");Iterator Itr2 = time.iterator();while (Itr2.hasNext()) {Element etime = (Element) Itr2.next();Element start = etime.element("start");Element end = etime.element("end");System.out.println("特殊开始时间" + start.getTextTrim());System.out.println("特殊结束时间" + end.getTextTrim());}}// 节假日holidayList lstHoliday = special.elements("holiday");// 所有的Item节点Iterator Itr3 = lstHoliday.iterator();while (Itr3.hasNext()) {Element eHoliday = (Element) Itr3.next();// 一个Item节点Element date = eHoliday.element("date");System.out.println("节假日===" + date.getTextTrim());}} catch (Exception e) {e.printStackTrace();}}}


运行结果
start.getTextTrim()=08:30end.getTextTrim()=12:00start.getTextTrim()=13:30end.getTextTrim()=17:30周六休息1周日休息1特殊工作日===2009-02-27特殊工作日===2009-04-28特殊开始时间10:00特殊结束时间12:00特殊工作日===2009-04-30特殊开始时间10:30特殊结束时间17:00节假日===2009-04-22节假日===2009-04-26节假日===2009-04-29

请勿发布不友善或者负能量的内容。与人为善,比聪明更重要!

留言需要登陆哦

技术博客集 - 网站简介:
前后端技术:
后端基于Hyperf2.1框架开发,前端使用Bootstrap可视化布局系统生成

网站主要作用:
1.编程技术分享及讨论交流,内置聊天系统;
2.测试交流框架问题,比如:Hyperf、Laravel、TP、beego;
3.本站数据是基于大数据采集等爬虫技术为基础助力分享知识,如有侵权请发邮件到站长邮箱,站长会尽快处理;
4.站长邮箱:[email protected];

      订阅博客周刊 去订阅

文章归档

文章标签

友情链接

Auther ·HouTiZong
侯体宗的博客
© 2020 zongscan.com
版权所有ICP证 : 粤ICP备20027696号
PHP交流群 也可以扫右边的二维码
侯体宗的博客