DOM4J介紹與代碼示例


DOM4J是dom4j.org出品的一個開源XML解析包。Dom4j是一個易用的、開源的庫,用於XML,XPath和XSLT。它應用於Java平台,采用了Java集合框架並完全支持DOM,SAX和JAXP。
DOM4J下載jar包:http://downloads.sourceforge.net/dom4j/dom4j-1.6.1.jar
JAXEN(對XPath的支持):http://dist.codehaus.org/jaxen/distributions/jaxen-1.1.1.zip
1.DOM4J 主要接口
DOM4J主要接口都在org.dom4j這個包里定義。
 
 
-Node為所有的dom4j中XML節點定義了多態行為;
 
-Branch為能夠包含子節點的節點如XML元素(Element)和文檔(Docuemnts)定義了一個公共的行為;
|-Element 定義XML 元素;
|-Document定義了XML文檔;
 
-DocumentType 定義XML DOCTYPE聲明;
-Entity定義 XML entity;
-Attribute定義了XML的屬性;
-ProcessingInstruction 定義 XML 處理指令;
 
-CharacterData是一個標識借口,標識基於字符的節點。如CDATA,Comment, Text;
|- CDATA 定義了XML CDATA 區域;
|-Text 定義XML 文本節點;
|- Comment 定義了XML注釋的行為;

 

項目結構

 

maven代碼

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>lqy</groupId>
  <artifactId>Dom4jTest</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>Dom4jTest</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>jaxen</groupId>
        <artifactId>jaxen</artifactId>
        <version>1.1.4</version>
    </dependency>
  </dependencies>
</project>

 

1.創建XML文檔

package lqy.Dom4jTest;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
 
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
 


/*
 * 創建XML文檔
 */
public class XmlGen {
    
    /*
     * 方法generateDocumentByMethod()通過調用方法構建xml文檔:
        1.使用DocumentHelper得到Document實例
        Document document = DocumentHelper.createDocument();
        2.創建Processing Instruction
        document.addProcessingInstruction("xml-stylesheet", inMap);
        3.創建元素Element
        Element studentsElement = document.addElement("students");
        4.為元素添加注釋Comment
        studentsElement.addComment("An Student Catalog");
        5.為元素添加屬性
        studentsElement.addComment("An Student Catalog");
        6.為元素添加文本值Text
        ageElement.setText("18");
     */
    public Document generateDocumentByMethod() {
        
       Document document = DocumentHelper.createDocument();
       // ProcessingInstruction
       Map<String, String> inMap = new HashMap<String, String>();
       inMap.put("type", "text/xsl");
       inMap.put("href", "students.xsl");
       document.addProcessingInstruction("xml-stylesheet", inMap);
       // root element
       Element studentsElement = document.addElement("students");
       studentsElement.addComment("An Student Catalog");
       // son element
       Element stuElement = studentsElement.addElement("student");
       stuElement.addAttribute("sn", "01");
       Element nameElement = stuElement.addElement("name");
       nameElement.setText("sam");
       Element ageElement = stuElement.addElement("age");
       ageElement.setText("18");
       // son element
       Element anotherStuElement = studentsElement.addElement("student");
       anotherStuElement.addAttribute("sn", "02");
       Element anotherNameElement = anotherStuElement.addElement("name");
       anotherNameElement.setText("lin");
       Element anotherAgeElement = anotherStuElement.addElement("age");
       anotherAgeElement.setText("20");
 
       return document;
    }
 
    
    /*
     * 方法generateDocumentByString()通過字符串轉換直接構建xml文檔,使用DocumentHelper.parseText()來實現.
     * document = DocumentHelper.parseText(text);
     */
    public Document generateDocumentByString() {
       String text = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
              "<?xml-stylesheet type=\"text/xsl\" href=\"students.xsl\"?>" +
              "<students><!--An Student Catalog-->   <student sn=\"01\">" +
              "<name>sam</name><age>18</age></student><student sn=\"02\">" +
              "<name>lin</name><age>20</age></student></students>";
       Document document = null;
       try {
           document = DocumentHelper.parseText(text);
       } catch (DocumentException e) {
           e.printStackTrace();
       }
       return document;
    }
 
    public void saveDocument(Document document, File outputXml) {
       try {
           // 美化格式
           OutputFormat format = OutputFormat.createPrettyPrint();
           /*// 縮減格式
           OutputFormat format = OutputFormat.createCompactFormat();*/
           /*// 指定XML編碼
            format.setEncoding("GBK");*/
           XMLWriter output = new XMLWriter(new FileWriter(outputXml), format);
           output.write(document);
           output.close();
       } catch (IOException e) {
           System.out.println(e.getMessage());
       }
    }
 
    public static void main(String[] argv) {
       XmlGen dom4j = new XmlGen();
       Document document = null;
        //通過方法生成
        document=dom4j.generateDocumentByMethod();
        
        //通過字符串生成
        //document = dom4j.generateDocumentByString();        
       dom4j.saveDocument(document, new File("students-gen.xml"));
    }
}

 

生成students-gen.xml如下

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>

<students>
  <!--An Student Catalog-->
  <student sn="01">
    <name>sam</name>
    <age>18</age>
  </student>
  <student sn="02">
    <name>lin</name>
    <age>20</age>
  </student>
</students>

 

方法generateDocumentByMethod()通過調用方法構建xml文檔:
1.使用DocumentHelper得到Document實例
Document document = DocumentHelper. createDocument();
2.創建Processing Instruction
document.addProcessingInstruction("xml-stylesheet", inMap);
3.創建元素Element
Element studentsElement = document.addElement("students");
4.為元素添加注釋Comment
studentsElement.addComment("An Student Catalog");
5.為元素添加屬性
studentsElement.addComment("An Student Catalog");
6.為元素添加文本值Text
ageElement.setText("18");
 
方法generateDocumentByString()通過字符串轉換直接構建xml文檔,使用DocumentHelper. parseText()來實現.
document = DocumentHelper. parseText(text);
 
方法saveDocument(Document document, File outputXml)將文檔輸出到文件保存,可指定字符編碼,可指定格式化輸出。

 

2.修改XML文檔

package lqy.Dom4jTest;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
 
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
 
/*
 * 這里使用xpath來定位待修改的元素和屬性,需要jaxen的支持。
 *示例中將students-gen.xml的第一個student元素的sn屬性改為001,其子元素name內容改為jeff。
 *XmlMod.java
 */
public class XmlMod {
    
    /*
     * 1.使用File定位文件資源,並基於此獲得Document實例
     *SAXReader saxReader = new SAXReader();
     *Document document = saxReader.read(inputXml);
     *2.Document實例的selectNodes方法可以傳入xpath,並返回一個List實例,基於此使用迭代器,完成特定的應用
     *List list = document.selectNodes("//students/student/@sn");
     */
    public void modifyDocument(File inputXml) {
       try {
           SAXReader saxReader = new SAXReader();
           Document document = saxReader.read(inputXml);
           
           List list = document.selectNodes("//students/student/@sn");
           Iterator iter = list.iterator();
           while (iter.hasNext()) {
              Attribute attribute = (Attribute) iter.next();
              if (attribute.getValue().equals("01"))
                  attribute.setValue("001");
           }
           list = document.selectNodes("//students/student");
           iter = list.iterator();
           while (iter.hasNext()) {
              Element element = (Element) iter.next();
              Iterator iterator = element.elementIterator("name");
              while (iterator.hasNext()) {
                  Element nameElement = (Element) iterator.next();
                  if (nameElement.getText().equals("sam"))
                     nameElement.setText("jeff");
              }
           }
           XMLWriter output = new XMLWriter(new FileWriter(new File(
                  "students-modified.xml")));
           output.write(document);
           output.close();
       }
 
       catch (DocumentException e) {
           System.out.println(e.getMessage());
       } catch (IOException e) {
           System.out.println(e.getMessage());
       }
    }
 
    /*
     * selectSingleNode如果有多個只取第一個
     */
    public void modifyDocument2(File inputXml){
        SAXReader saxReader = new SAXReader();
        try {
            Document document = saxReader.read(inputXml);
            Node nodeAttr=document.selectSingleNode("//students/student/@sn");
            System.out.println(nodeAttr.getText());
            nodeAttr.setText("nodeAttr");
            
            Node nodeEle=document.selectSingleNode("//students/student");
            System.out.println(nodeEle.getText());
            nodeEle.setText("nodeEle");    
            
               XMLWriter output = new XMLWriter(new FileWriter(new File(
                       "students-modified2.xml")));
                output.write(document);            
                output.close();        
        } catch (Exception e) {
            e.printStackTrace();
        }
        
        
    }
    
    
    
    
    public static void main(String[] argv) {
       XmlMod dom4jParser = new XmlMod();
       //dom4jParser.modifyDocument(new File("students-gen.xml"));
       dom4jParser. modifyDocument2(new File("students-gen.xml"));
    }
}

 

生成students-modified.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?><students>
  <!--An Student Catalog-->  
  <student sn="001">
    <name>jeff</name>
    <age>18</age>
  </student>
  <student sn="02">
    <name>lin</name>
    <age>20</age>
  </student>
</students>

生成students-modified2.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?><students>
  <!--An Student Catalog-->  
  <student sn="nodeAttr"><name>sam</name><age>18</age>nodeEle</student>
  <student sn="02">
    <name>lin</name>
    <age>20</age>
  </student>
</students>

 

1.使用File定位文件資源,並基於此獲得Document實例
SAXReader saxReader = new SAXReader();
Document document = saxReader.read(inputXml);
2.Document實例的selectNodes方法可以傳入xpath,並返回一個List實例,基於此使用迭代器,完成特定的應用
List list = document.selectNodes("//students/student/@sn");

 

3.遍歷XML文檔

這里提供兩種遍歷方法,一種是基於迭代的遍歷,一種是基於Visitor模式的遍歷。 這里介紹迭代的

 

package lqy.Dom4jTest;

import java.io.File;
import java.util.Iterator;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/*
 * 方法traversalDocumentByIterator()提供一種基於迭代的遍歷實現,每個Element通過elementIterator()和attributeIterator()取代其子元素和屬性的迭代器。
 */
public class XmlIterator {

    private File inputXml;
    
    public XmlIterator(File inputXml) {
       this.inputXml = inputXml;
    }

    public static void main(String[] argv) {
        XmlIterator dom4jParser = new XmlIterator(new File("students-gen.xml"));
        dom4jParser.traversalDocumentByIterator();
     }
    
    
    
    public Element getRootElement() {
        return getDocument().getRootElement();
     }
    
    public Document getDocument() {
        SAXReader saxReader = new SAXReader();
        Document document = null;
        try {
            document = saxReader.read(inputXml);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return document;
     }
    
    /*
     * 該方法只是枚舉了兩層,如果一直挖的話要用遞歸方法
     */
    public void traversalDocumentByIterator() {
        Element root = getRootElement();
        // 枚舉根節點下所有子節點
        for (Iterator ie = root.elementIterator(); ie.hasNext();) {
            System.out.println("======");
            Element element = (Element) ie.next();
            System.out.println(element.getName());
  
            // 枚舉屬性
            for (Iterator ia = element.attributeIterator(); ia.hasNext();) {
               Attribute attribute = (Attribute) ia.next();
               System.out.println(attribute.getName() + ":"
                      + attribute.getData());
            }
            
            // 枚舉當前節點下所有子節點
            for (Iterator ieson = element.elementIterator(); ieson.hasNext();) {
               Element elementSon = (Element) ieson.next();
               System.out.println(elementSon.getName() + ":"+ elementSon.getText());
            }
        }
     }
    
    
    
    
}

 

輸出

 

 

4.通過Element找到節點

package lqy.Dom4jTest;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class XmlReadByElement {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {

        XmlReadByElement.getOneEle();
        XmlReadByElement.getEles();
        XmlReadByElement.getAttrs();
        XmlReadByElement.getOneAttr();
    }
    
    
    public static Document getDocument() throws Exception{
        SAXReader reader = new SAXReader();
        Document  document = reader.read(new File("students-read.xml"));
        return document;
    }
    
    //要順序一個一個往下找
    public static void getOneEle() throws Exception{
        Document  document=getDocument();
        Element rootElm = document.getRootElement();
        Element e=rootElm.element("lqy");
        System.out.println("arrt:"+e.attributeValue("aa"));
        Element e2=e.element("login");
        System.out.println("text:"+e2.getText());
        
        
    }
    
    public static void getEles() throws Exception{
        Document  document=getDocument();
        Element rootElm = document.getRootElement();
        List eList=rootElm.elements("student");
        Iterator it=eList.iterator();
        while(it.hasNext()){
            Element elm=(Element)it.next();
            System.out.println("arrt:"+elm.attributeValue("sn"));
        }

    }
    
    
    public static void getOneAttr() throws Exception{
        Document  document=getDocument();
        Node node1=document.selectSingleNode("//students/lqy/login");
        Element elm=(Element)node1;
        System.out.println("attr:"+elm.attributeValue("sn"));
        
    }
    
    public static void getAttrs() throws Exception{
        Document  document=getDocument();
        List list = document.selectNodes("//students/student");
        Iterator it=list.iterator();
        while(it.hasNext()){
            Element elm=(Element)it.next();
            System.out.println("attr:"+elm.attributeValue("sn"));
        }

    }
   

}

 

students-read.xml

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="students.xsl"?>

<students>
  <!--An Student Catalog-->
  <student sn="01">
    <name>sam</name>
    <age>18</age>
  </student>
  <student sn="02">
    <name>lin</name>
    <age>20</age>
  </student>
    <lqy aa="123123123">
    <login sn="03">luo</login>
    <pass>123456</pass>
  </lqy>
</students>

輸出結果

 

 

5.通過XPath找到節點

package lqy.Dom4jTest;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class XmlReadByXPath {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {

        XmlReadByXPath.getOneEleByXPath();
        XmlReadByXPath.getElesByXPath();
        XmlReadByXPath.getAttrsByXPath();
        XmlReadByXPath.getOneAttrByXPath();
    }
    
    
    public static Document getDocument() throws Exception{
        SAXReader reader = new SAXReader();
        Document  document = reader.read(new File("students-read.xml"));
        return document;
    }
    
    public static void getOneEleByXPath() throws Exception{
        Document  document=getDocument();
        Node node1=document.selectSingleNode("//students/lqy/login");
        String nodeText1=node1.getText();
        System.out.println("nodeText1:"+nodeText1);
        
        //假如多個只返回第一個
        Node node2=document.selectSingleNode("//students/student/name");
        String nodeText2=node2.getText();
        System.out.println("nodeText2:"+nodeText2);
        
        Node node3=document.selectSingleNode("//students/student/@sn");
        short nodeText3=node1.ATTRIBUTE_NODE;
        System.out.println("nodeText3:"+nodeText3);
        
    }
    
    public static void getElesByXPath() throws Exception{
        Document  document=getDocument();
        List list = document.selectNodes("//students/student/name");
        Iterator it=list.iterator();
        while(it.hasNext()){
            Element elm=(Element)it.next();
             System.out.println("elm:"+elm.getText());
        }

    }
    
    
    public static void getOneAttrByXPath() throws Exception{
        Document  document=getDocument();
        Node node1=document.selectSingleNode("//students/lqy/login");
        Element elm=(Element)node1;
        System.out.println("attr:"+elm.attributeValue("sn"));
        
    }
    
    public static void getAttrsByXPath() throws Exception{
        Document  document=getDocument();
        List list = document.selectNodes("//students/student");
        Iterator it=list.iterator();
        while(it.hasNext()){
            Element elm=(Element)it.next();
            System.out.println("attr:"+elm.attributeValue("sn"));
        }

    }
    
    

}

輸出結果

 

 

 

6.XML轉換成String

 

package lqy.Dom4jTest;

import java.io.File;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

public class XMLToString {

    /**
     * @param args
     * @throws Exception 
     */
    public static void main(String[] args) throws Exception {
        SAXReader reader = new SAXReader();
        Document  document = reader.read(new File("students-read.xml"));        
        String docXmlText=document.asXML();
        System.out.println(docXmlText);
        System.out.println("------------------docXmlText-------------------");
        Element root=document.getRootElement();   
        String rootXmlText=root.asXML();
        System.out.println(rootXmlText);
        System.out.println("------------------rootXmlText-------------------");
        Element memberElm=root.element("lqy");
        String memberXmlText=memberElm.asXML();
        System.out.println(memberXmlText);
        System.out.println("------------------memberXmlText-------------------");

    }
        

}

 

 輸出

 

 

參考

1.http://zhangjunhd.blog.51cto.com/113473/126310

2.http://www.blogjava.net/junglesong/archive/2008/02/21/181196.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM