一、到官网下载Dom4j(  http://sourceforge.net/projects/dom4j/ ),并解压,Dom4j读操作需要如下jar文件:
          dom4j-1.6.1.jar        
          jaxen-1.1-beta-6.jar
 
二、Eclipse 创建一个Web Project("testform1"),并将上面的两个jar文件拷贝到项目classpath下,并在项目根目录下创建一个xml( book.xml ),整个项目结构如下图:
dom4j图解读取文档操作_第1张图片
 
 
2.1: book.xml内容如下:
xml version ="1.0" encoding ="UTF-8" ?>
< booklist >
   < cbook cid ="0001" >
    cbook1
     < bookname >XML的今生今世 bookname >
     < price >66.66 price >
     < note >该书详细描写XML的从出生到老的整个过程 note >    
     < newBook >
       < cbook cid ="0001" >
         < bookname >XML教程0001 bookname >
         < price >90 price >
         < note >该书详细描写XML0001 note >
       cbook >
     newBook >
   cbook >
        
   < cbook cid ="0002" >
    cbook2
     < bookname >XML的今生今世 bookname >
     < price >68.68 price >
     < note >该书详细描写XML的从出生到老的整个过程 note >    
     < newBook >
       < cbook cid ="0002" >
         < bookname >XML教程0 bookname >
         < price >90 price >
         < note >该书详细描写XML note >
       cbook >
     newBook >
   cbook >
    
booklist >
 
2.2:创建一个java类:TestXMLDemo.java
 
package com.lixing.testxml;

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

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

public class TestXMLDemo {
   public void myXMLReader() throws Exception{
    File file= new File( "book.xml");
    SAXReader reader= new SAXReader();
    Document doc=reader.read(file);     //获取整个XML文档
    Element root=doc.getRootElement();   //获得根元素

    Element cbook=root.element( "cbook");   //默认获取得一个cbook节点s
    System.out.println(cbook.attributeValue( "cid"));
    
    Attribute cid=cbook.attribute( "cid");   //获取cbook节点中cid属性
    System.out.println(cid.getText());
    
    System.out.println(cbook.getText());   //获取cbook第一个标签内文本内容
    Element newBook=cbook.element( "newBook");
    Element newBook_cbook=newBook.element( "cbook");
    Element note=newBook_cbook.element( "note");
    System.out.println(note.getText());
  }
    
   public static void main(String[] args) throws Exception{
    TestXMLDemo demo= new TestXMLDemo();
    demo.myXMLReader();
  }
}
以上提供的方法只能读取第一个子节点的内容,那么如何获取整遍文档的内容呢?
 
2.3:创建一个Java类:TextXMLDemo.java
package com.lixing.testxml;

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

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

public class TestXMLDemo {
   public void myXMLReader() throws Exception{
    File file= new File( "book.xml");
    SAXReader reader= new SAXReader();
    Document doc=reader.read(file);     //获取整个XML文档
    Element root=doc.getRootElement();   //获得根元素
    
    List list=root.elements( "cbook");     //获取根元素下所有的cbook子元素
    Iterator it=list.iterator();
     while(it.hasNext()){
      Element tempElement=it.next();
      Element bookName=tempElement.element( "bookname");
      Element price=tempElement.element( "price");
      Element note=tempElement.element( "note");
      System.out.println(bookName.getText());
      System.out.println(price.getText());
      System.out.println(note.getText()+ "\n");
    }
    
  }
    
   public static void main(String[] args) throws Exception{
    TestXMLDemo demo= new TestXMLDemo();
    demo.myXMLReader();
  }
}