主要类:Demo1.java,一个DOM4J实例,详细解说怎么用DOM4J编程 package com.demo; import java.io.FileOutputStream; import java.io.OutputStreamWriter; import java.util.List; 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.SAXReader; import org.dom4j.io.XMLWriter; import org.junit.Test; public class Demo1 { // 获取第一个书名节点的值 @Test public void read() throws DocumentException{ SAXReader reader = new SAXReader(); Document document = reader.read("src/book.xml"); String value = document.getRootElement().element("book").element("bookname").getText(); System.out.println(value); } // 获取第二个书名节点的值 @Test public void read1() throws DocumentException{ SAXReader reader = new SAXReader(); Document document = reader.read("src/book.xml"); Element root = document.getRootElement(); Element book = (Element)root.elements("book").get(1); Element bookname = book.element("bookname"); System.out.println(bookname.getText()); } // 获取书名节点的属性值 @Test public void read2() throws DocumentException{ SAXReader reader = new SAXReader(); Document document = reader.read("src/book.xml"); Element root = document.getRootElement(); String value = root.element("book").element("bookname").attributeValue("id"); System.out.println(value); } // 修改 @Test public void update() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read("src/book.xml"); Element root = document.getRootElement(); root.element("book").element("price").setText("60元"); /** * 如果xml文件中有中文则需要转码,纯英文编码不用转 * XMLWriter writer = new XMLWriter(new FileWriter("src/book.xml")); writer.write(document); writer.close(); 转码加入xml文件的一种方法 XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF-8")); writer.write(document); writer.close(); * */ //转码加入xml文件的另一种方法 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format); writer.write(document); writer.close(); } // 删除 @Test public void delete() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read("src/book.xml"); Element root = document.getRootElement(); Element price = root.element("book").element("price"); price.getParent().remove(price); //转码加入xml文件的一种方法 XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/book.xml"),"UTF-8")); writer.write(document); writer.close(); } //添加 给第一本书添加一个售价 @Test public void insert() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read("src/book.xml"); Element root = document.getRootElement(); //创建一个售价节点 Element price = DocumentHelper.createElement("售价"); price.setText("40元"); root.element("book").add(price); //转码加入xml文件的另一种方法 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format); writer.write(document); writer.close(); } //指定位置添加 给第一本书添加一个售价 @Test public void insert1() throws Exception{ SAXReader reader = new SAXReader(); Document document = reader.read("src/book.xml"); Element root = document.getRootElement(); //创建一个售价节点 Element price = DocumentHelper.createElement("售价"); price.setText("50元"); //指定位置添加,bookname和author中间的位置 List list = root.element("book").elements(); list.add(1,price); //转码加入xml文件的另一种方法 OutputFormat format = OutputFormat.createPrettyPrint(); format.setEncoding("UTF-8"); XMLWriter writer = new XMLWriter(new FileOutputStream("src/book.xml"),format); writer.write(document); writer.close(); } }