首页 > 代码库 > Dom4j解析XML

Dom4j解析XML

1、Dom4j概述

dom4j is an easy to use, open source library for working with XML, XPath and XSLT on the Java platform using the Java Collections Framework and with full support for DOM, SAX and JAXP. 

dom4j官方网址:dom4j

dom4j源码下载:dom4j download

本示例中,需要导入dom4j.jar包,才能引用dom4j相关类,dom4j源码和jar包,请见本示例【源码下载】或访问 dom4j

 

org.dom4j包,不仅包含创建xml的构建器类DocumentHelper、Element,而且还包含解析xml的解析器SAXReader、Element,包含类如下:

org.dom4j

org.dom4j.DocumentHelper;

org.dom4j.Element;

org.dom4j.io.SAXReader;

org.dom4j.io.XMLWriter;

org.dom4j.DocumentException;

sdk源码查看路径(google code)

 

创建和解析xml的效果图:

 

 

2、Dom4j 创建 XML

Dom4j,创建xml主要用到了org.dom4j.DocumentHelper、org.dom4j.Document、org.dom4j.io.OutputFormat、org.dom4j.io.XMLWriter

首先,DocumentHelper.createDocument(),创建 org.dom4j.Document 的实例 doc

接着,通过doc,设置xml属性doc.setXMLEncoding("utf-8")、doc.addElement("root")根节点,以及子节点等

然后,定义xml格式并输出,new XMLWriter(xmlWriter, outputFormat)

Code

[java] view plaincopyprint?
  1. /** Dom4j方式,创建 XML  */  
  2. public String dom4jXMLCreate(){  
  3.     StringWriter xmlWriter = new StringWriter();  
  4.   
  5.     Person []persons = new Person[3];       // 创建节点Person对象  
  6.     persons[0] = new Person(1, "sunboy_2050", "http://blog.csdn.net/sunboy_2050");  
  7.     persons[1] = new Person(2, "baidu", "http://www.baidu.com");  
  8.     persons[2] = new Person(3, "google", "http://www.google.com");  
  9.       
  10.     try {  
  11.         org.dom4j.Document doc = DocumentHelper.createDocument();  
  12.           
  13.         doc.setXMLEncoding("utf-8");  
  14.           
  15.         org.dom4j.Element eleRoot = doc.addElement("root");  
  16.         eleRoot.addAttribute("author", "homer");  
  17.         eleRoot.addAttribute("date", "2012-04-25");  
  18.         eleRoot.addComment("dom4j test");  
  19.           
  20.         int personsLen = persons.length;  
  21.         for(int i=0; i<personsLen; i++){  
  22.               
  23.             Element elePerson = eleRoot.addElement("person");   // 创建person节点,引用类为 org.dom4j.Element  
  24.               
  25.             Element eleId = elePerson.addElement("id");  
  26.             eleId.addText(persons[i].getId()+"");  
  27.               
  28.             Element eleName = elePerson.addElement("name");  
  29.             eleName.addText(persons[i].getName());  
  30.               
  31.             Element eleBlog = elePerson.addElement("blog");  
  32.             eleBlog.addText(persons[i].getBlog());  
  33.         }  
  34.   
  35.         org.dom4j.io.OutputFormat outputFormat = new org.dom4j.io.OutputFormat();   // 设置xml输出格式  
  36.         outputFormat.setEncoding("utf-8");  
  37.         outputFormat.setIndent(false);  
  38.         outputFormat.setNewlines(true);  
  39.         outputFormat.setTrimText(true);  
  40.           
  41.         org.dom4j.io.XMLWriter output = new XMLWriter(xmlWriter, outputFormat);     // 保存xml  
  42.         output.write(doc);  
  43.         output.close();  
  44.     } catch (Exception e) {  
  45.         e.printStackTrace();  
  46.     }  
  47.       
  48.     savedXML(fileName, xmlWriter.toString());  
  49.     return xmlWriter.toString();  
  50. }  


运行结果:

 

 

3、Dom4j 解析 XML

Dom4j,解析xml主要用到了org.dom4j.io.SAXReader、org.dom4j.Document、doc.getRootElement(),以及ele.getName()、ele.getText()等

首先,创建SAXReader的实例reader,读入xml字节流 reader.read(is)

接着,通过doc.getRootElement()得到root根节点,利用迭代器取得root下一级的子节点eleRoot.elementIterator()等

然后,得到解析的xml内容xmlWriter.append(xmlHeader)、xmlWriter.append(personsList.get(i).toString())

 

解析一:标准解析(Iterator 迭代)

Code

[java] view plaincopyprint?
  1. /** Dom4j方式,解析 XML  */  
  2. public String dom4jXMLResolve(){  
  3.     StringWriter xmlWriter = new StringWriter();  
  4.       
  5.     InputStream is = readXML(fileName);  
  6.     try {  
  7.         SAXReader reader = new SAXReader();  
  8.         org.dom4j.Document doc = reader.read(is);  
  9.   
  10.         List<Person> personsList = null;  
  11.         Person person = null;  
  12.         StringBuffer xmlHeader = new StringBuffer();  
  13.           
  14.           
  15.         Element eleRoot = doc.getRootElement();     // 获得root根节点,引用类为 org.dom4j.Element  
  16.         String attrAuthor = eleRoot.attributeValue("author");  
  17.         String attrDate = eleRoot.attributeValue("date");  
  18.         xmlHeader.append("root").append("\t\t");  
  19.         xmlHeader.append(attrAuthor).append("\t");  
  20.         xmlHeader.append(attrDate).append("\n");  
  21.         personsList = new ArrayList<Person>();  
  22.           
  23.         // 获取root子节点,即person  
  24.         Iterator<Element> iter = eleRoot.elementIterator();  
  25.         for(; iter.hasNext(); ) {  
  26.             Element elePerson = (Element)iter.next();  
  27.               
  28.             if("person".equals(elePerson.getName())){  
  29.                 person = new Person();  
  30.                   
  31.                 // 获取person子节点,即id、name、blog  
  32.                 Iterator<Element> innerIter = elePerson.elementIterator();  
  33.                 for(; innerIter.hasNext();) {  
  34.                     Element ele = (Element)innerIter.next();  
  35.                       
  36.                     if("id".equals(ele.getName())) {  
  37.                         String id = ele.getText();  
  38.                         person.setId(Integer.parseInt(id));  
  39.                     } else if("name".equals(ele.getName())) {  
  40.                         String name = ele.getText();  
  41.                         person.setName(name);  
  42.                     } else if("blog".equals(ele.getName())) {  
  43.                         String blog = ele.getText();  
  44.                         person.setBlog(blog);  
  45.                     }  
  46.                 }  
  47.                   
  48.                 personsList.add(person);  
  49.                 person = null;  
  50.             }  
  51.         }  
  52.           
  53.         xmlWriter.append(xmlHeader);  
  54.         int personsLen = personsList.size();  
  55.         for(int i=0; i<personsLen; i++) {  
  56.             xmlWriter.append(personsList.get(i).toString());  
  57.         }  
  58.           
  59.     } catch (DocumentException e) {  
  60.         e.printStackTrace();  
  61.     } catch (Exception e) {  
  62.         e.printStackTrace();  
  63.     }  
  64.       
  65.     return xmlWriter.toString();  
  66. }  

 

运行结果:

 

 

解析二:选择性解析(XPath路径

Dom4j+XPath,选择性只解析id,doc.selectNodes("//root//person//id")

Code

[java] view plaincopyprint?
  1. /** Dom4j方式,解析 XML(方式二)  */  
  2. public String dom4jXMLResolve2(){  
  3.     StringWriter xmlWriter = new StringWriter();  
  4.       
  5.     InputStream is = readXML(fileName);  
  6.     try {  
  7.         org.dom4j.io.SAXReader reader = new org.dom4j.io.SAXReader();  
  8.         org.dom4j.Document doc = reader.read(is);  
  9.   
  10.         List<Person> personsList = null;  
  11.         Person person = null;  
  12.         StringBuffer xmlHeader = new StringBuffer();  
  13.           
  14.           
  15.         Element eleRoot = doc.getRootElement();     // 获得root根节点,引用类为 org.dom4j.Element  
  16.         String attrAuthor = eleRoot.attributeValue("author");  
  17.         String attrDate = eleRoot.attributeValue("date");  
  18.         xmlHeader.append("root").append("\t\t");  
  19.         xmlHeader.append(attrAuthor).append("\t");  
  20.         xmlHeader.append(attrDate).append("\n");  
  21.         personsList = new ArrayList<Person>();  
  22.           
  23.         @SuppressWarnings("unchecked")  
  24.         List<Element> idList = (List<Element>) doc.selectNodes("//root//person//id");   // 选择性获取全部id  
  25.         Iterator<Element> idIter = idList.iterator();  
  26.         while(idIter.hasNext()){  
  27.             person = new Person();  
  28.               
  29.             Element idEle = (Element)idIter.next();  
  30.             String id = idEle.getText();  
  31.             person.setId(Integer.parseInt(id));  
  32.               
  33.             personsList.add(person);  
  34.         }  
  35.   
  36.         xmlWriter.append(xmlHeader);  
  37.         int personsLen = personsList.size();  
  38.         for(int i=0; i<personsLen; i++) {  
  39.             xmlWriter.append("id = ").append(personsList.get(i).getId()+"").append("\n");  
  40.         }  
  41.           
  42.     } catch (DocumentException e) {  
  43.         e.printStackTrace();  
  44.     } catch (Exception e) {  
  45.         e.printStackTrace();  
  46.     }  
  47.       
  48.     return xmlWriter.toString();  
  49. }  

注:借助 XPath 解析 XML 时,需要导入 jaxen;本示例需要导入的是最新的jaxen包jaxen-1.1.3.jar,可以下载本示例下面【源码下载】或 访问 jaxen jar

Jaxen is an open source XPath library written in Java. It is adaptable to many different object models, including DOM, XOM, dom4j, and JDOM. Is it also possible to write adapters that treat non-XML trees such as compiled Java byte code or Java beans as XML, thus enabling you to query these trees with XPath too.

jaxen 官方网址:jaxen

jaxen下载jar包:jaxen jar 或 jaxen jar

jaxen源码查看:jaxen src 或 jaxen trunk

 

运行结果:

 

 

 

4、Person类

请参见前面博客 Android 创建与解析XML(二)—— Dom方式 【4、Person类】

 

 

源码下载

 

 

参考推荐:

dom4j(官方网站)

dom4j src(源码下载)

dom4j src and jar(google code)

 

jaxen(jaxen 官方网址)

jaxen jar(jaxen jar包下载)

jaxen src(jaxen在线源码)

 

dom4j 解析 XML(IBM)

dom4j和XPath解析XML

dom4j 属性值回车换行问题