当前位置:文档之家› Dom4j遍历解析XML测试

Dom4j遍历解析XML测试

Dom4j遍历解析XML测试近来老和XML打交道,不深挖不行了。

这是一个Dom4j解析XML的例子,为做复杂递归处理前期所写的例子。

涵盖了XML的解析方面大部分核心API。

环境:Dom4j-1.6.1Dom4j解析需要XML需要的最小类库为:dom4j-1.6.1.jarjaxen-1.1-beta-6.jar目标:解析一个xml,输出所有的属性和元素值。

测试代码:XML文件:<?xml version="1.0"encoding="GBK"?><doc><person id="1"sex="m"><name>zhangsan</name><age>32</age><adds><add code="home">home add</add><add code="com">com add</add></adds></person><person id="2"sex="w"><name>lisi</name><age>22</age><adds><add ID="22"id="23"code="home">home add</add><add ID="23"id="22"code="com">com add</add><add id="24"code="com">com add</add></adds></person></doc>解析代码:package com.topsoft.test;import org.dom4j.io.SAXReader;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.Node;import java.util.Iterator;import java.util.List;import java.io.InputStream;/*** Created by IntelliJ IDEA.<br>* <b>User</b>: leizhimin<br>* <b>Date</b>: 2008-3-26 15:53:51<br>* <b>Note</b>: Dom4j遍历解析XML测试*/public class TestDom4j {/*** 获取指定xml文档的Docum ent对象,xml文件必须在classpath中可以找到** @param xmlFilePath xml文件路径* @return Document对象*/public static Docum ent parse2Document(String xmlFilePath) {SAXReader reader = new SAXReader();Docum ent docum ent = null;try {InputStream in =TestDom4j.class.getResourceAsStream(xmlFilePath); docum ent =reader.read(in);} catch (Docum entException e) {System.out.println(e.getMessage());System.out.println("读取classpath下xmlFileName文件发生异常,请检查CL ASSPATH和文件名是否存在!");e.printStackTrace();}return docum ent;}public static void testParseXMLData(String xmlFileName) {//产生一个解析器对象SAXReader reader = new SAXReader();//将xml文档转换为Docum ent的对象Docum ent docum ent =parse2Docum ent(xmlFileNam e);//获取文档的根元素Elem ent root =docum ent.getRootElement();//定义个保存输出xml数据的缓冲字符串对象StringBuffer sb = new StringBuffer();sb.append("通过Dom4j解析XML,并输出数据:\n");sb.append(xmlFileName + "\n");sb.append("----------------遍历start----------------\n");//遍历当前元素(在此是根元素)的子元素for (Iterator i_pe = root.elem entIterator(); i_pe.hasNext();) {Elem ent e_pe =(Element) i_pe.next();//获取当前元素的名字String person =e_pe.getName();//获取当前元素的id和sex属性的值并分别赋给id,sex变量String id = e_pe.attributeValue("id");String sex =e_pe.attributeValue("sex");String name =e_pe.element("name").getText();String age =e_pe.element("age").getText();//将数据存放到缓冲区字符串对象中sb.append(person + ":\n");sb.append("\tid=" + id + " sex=" + sex + "\n");sb.append("\t" + "name=" +name + " age=" +age + "\n");//获取当前元素e_pe(在此是person元素)下的子元素addsElem ent e_adds =e_pe.element("adds");sb.append("\t" + e_adds.getName() + "\n");//遍历当前元素e_adds(在此是adds元素)的子元素for (Iterator i_adds =e_adds.elementIterator(); i_adds.hasNext();) { Elem ent e_add = (Element) i_adds.next();String code =e_add.attributeValue("code");String add =e_add.getTextTrim();sb.append("\t\t" +e_add.getName() + ":" +" code=" +code + " va lue=\"" + add + "\"\n");}sb.append("\n");}sb.append("-----------------遍历end-----------------\n");System.out.println(sb.toString());System.out.println("---------通过XPath获取一个元素----------");Node node1 =document.selectSingleNode("/doc/person");System.out.println("输出节点:" +"\t"+node1.asXML());Node node2 =document.selectSingleNode("/doc/person/@sex");System.out.println("输出节点:" +"\t"+node2.asXML());Node node3 =document.selectSingleNode("/doc/person[name=\"zhangsan \"]/age");System.out.println("输出节点:" +"\t"+node3.asXML());System.out.println("\n---------XPath获取List节点测试------------");List list =docum ent.selectNodes("/doc/person[name=\"zhangsan\"]/adds/ add");for(Iterator it=list.iterator();it.hasNext();){Node nodex=(Node)it.next();System.out.println(nodex.asXML());}System.out.println("\n---------通过ID获取元素的测试----------");System.out.println("陷阱:通过ID获取,元素ID属性名必须为“大写ID”,小写的“i d”会认为是普通属性!");String id22 =document.elementByID("22").asXML();String id23 =document.elementByID("23").asXML();String id24 = null;if (docum ent.elem entByID("24") != null) {id24 =docum ent.elem entByID("24").asXML();} else {id24 ="null";}System.out.println("id22= " +id22);System.out.println("id23= " +id23);System.out.println("id24= " +id24);}public static void m ain(String args[]) {testParseXMLData("/person.xml");}}运行结果:通过Dom4j解析XML,并输出数据:/person.xml----------------遍历start---------------- person:id=1 sex=mnam e=zhangsan age=32addsadd: code=hom e value="home add"add: code=com value="com add"person:id=2 sex=wnam e=lisi age=22addsadd: code=hom e value="home add"add: code=com value="com add"add: code=com value="com add"-----------------遍历end--------------------------通过XPath获取一个元素----------输出节点: <person id="1"sex="m"><name>zhangsan</name><age>32</age><adds><add code="home">home add</add><add code="com">com add</add> </adds></person>输出节点: sex="m"输出节点: <age>32</age>---------XPath获取List节点测试------------<add code="home">home add</add><add code="com">com add</add>---------通过ID获取元素的测试----------陷阱:通过ID获取,元素ID属性名必须为“大写ID”,小写的“id”会认为是普通属性!id22= <add ID="22"id="23"code="home">home add</add>id23= <add ID="23"id="22"code="com">com add</add>id24= nullProcess finished with exit code 0发个Idea7开发界面截图: 点击图片放大想从头了解dom4j的朋友可以看dom4j文档中的quick start,这个是E文版的,另外有热心的网友已经将自己翻译的中文版奉献出来了,可以看看下面内容DOM4J是出品的一个开源XML解析包,它的网站中这样定义:Dom4j是一个易用的、开源的库,用于XML,XPath和XSLT。

相关主题