---
title: "DOM4JDemo"
author: "wangyu070707"
date: "2010-05-18T08:49:59Z"
tags: []
categories: ["编程语言"]
slug: "dom4jdemo"
draft: false
lastmod: "2010-05-18T08:49:59Z"
id: 668649
url: "https://fuwu58.iteye.com/blog/668649"
---

```java
package com.joyque.biap.test;
import java.io.File;
import java.io.FileWriter;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;


/**
 * 
 */
public class Dom4jDemo {

 public Dom4jDemo() {

 }

 /**
  * 建立一个XML文档,文档名由输入参数决定
  * 
  * @param filename
  *            需建立的文件名
  * @return 返回操作结果, 0表失败, 1表成功
  */

 public int createXMLFile(String filename) {
  /** 返回操作结果, 0表失败, 1表成功 */
  int returnValue = 0;

  /** 建立document对象 */
  Document document = DocumentHelper.createDocument();
  
  /** 建立XML文档的根books */
  Element booksElement = document.addElement(&quot;books&quot;);
  
  /** 加入一行注释 */
  booksElement.addComment(&quot;This is a test for dom4j, holen, 2004.9.11&quot;);
  
  /** 加入第一个book节点 */
  Element bookElement = booksElement.addElement(&quot;book&quot;);
  
  /** 加入show参数内容 */
  bookElement.addAttribute(&quot;show&quot;, &quot;yes&quot;);
  
  /** 加入title节点 */
  Element titleElement = bookElement.addElement(&quot;title&quot;);
  
  /** 为title设置内容 */
  titleElement.setText(&quot;Dom4j Tutorials&quot;);
  
  /** 类似的完成后两个book */
  bookElement = booksElement.addElement(&quot;book&quot;);
  bookElement.addAttribute(&quot;show&quot;, &quot;yes&quot;);
  titleElement = bookElement.addElement(&quot;title&quot;);
  titleElement.setText(&quot;Lucene Studing&quot;);
  bookElement = booksElement.addElement(&quot;book&quot;);
  bookElement.addAttribute(&quot;show&quot;, &quot;no&quot;);
  titleElement = bookElement.addElement(&quot;title&quot;);
  titleElement.setText(&quot;Lucene in Action&quot;);
  
  /** 加入owner节点 */
  Element ownerElement = booksElement.addElement(&quot;owner&quot;);
  ownerElement.setText(&quot;O'Reilly&quot;);
  try {
   /** 将document中的内容写入文件中 */
   XMLWriter writer = new XMLWriter(new FileWriter(new File(filename)));
   writer.write(document);
   writer.close();
   
   /** 执行成功,需返回1 */
   returnValue = 1;
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return returnValue;
 }

 /**
  * 修改XML文件中内容,并另存为一个新文件 重点掌握dom4j中如何添加节点,修改节点,删除节点
  * 
  * @param filename
  *            修改对象文件
  * @param newfilename
  *            修改后另存为该文件
  * @return 返回操作结果, 0表失败, 1表成功
  */

 public int ModiXMLFile(String filename, String newfilename) {
  int returnValue = 0;
  try {
   SAXReader saxReader = new SAXReader();
   Document document = saxReader.read(new File(filename));
   /** 修改内容之一: 如果book节点中show参数的内容为yes,则修改成no */
   /** 先用xpath查找对象 */
   List list = document.selectNodes(&quot;/books/book/@show&quot;);
   Iterator iter = list.iterator();
   while (iter.hasNext()) {
    Attribute attribute = (Attribute) iter.next();
    if (attribute.getValue().equals(&quot;yes&quot;)) {
     attribute.setValue(&quot;no&quot;);
    }
   }

   /**
    * 修改内容之二: 把owner项内容改为Tshinghua
    * 并在owner节点中加入date节点,date节点的内容为2004-09-11,还为date节点添加一个参数type
    */
   list = document.selectNodes(&quot;/books/owner&quot;);
   iter = list.iterator();
   if (iter.hasNext()) {
    Element ownerElement = (Element) iter.next();
    ownerElement.setText(&quot;Tshinghua&quot;);
    Element dateElement = ownerElement.addElement(&quot;date&quot;);
    dateElement.setText(&quot;2004-09-11&quot;);
    dateElement.addAttribute(&quot;type&quot;, &quot;Gregorian calendar&quot;);
   }

   /** 修改内容之三: 若title内容为Dom4j Tutorials,则删除该节点 */
   list = document.selectNodes(&quot;/books/book&quot;);
   iter = list.iterator();
   while (iter.hasNext()) {
    Element bookElement = (Element) iter.next();
    Iterator iterator = bookElement.elementIterator(&quot;title&quot;);
    while (iterator.hasNext()) {
     Element titleElement = (Element) iterator.next();
     if (titleElement.getText().equals(&quot;Dom4j Tutorials&quot;)) {
      bookElement.remove(titleElement);
     }
    }
   }

   try {
    /** 将document中的内容写入文件中 */
    XMLWriter writer = new XMLWriter(new FileWriter(new File(
      newfilename)));
    writer.write(document);
    writer.close();

    /** 执行成功,需返回1 */
    returnValue = 1;

   } catch (Exception ex) {
    ex.printStackTrace();
   }
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return returnValue;
 }

 /**
  * 格式化XML文档,并解决中文问题
  * 
  * @param filename
  * @return
  */
 public int formatXMLFile(String filename) {
  int returnValue = 0;
  try {
   SAXReader saxReader = new SAXReader();
   Document document = saxReader.read(new File(filename));
   XMLWriter output = null;

   /** 格式化输出,类型IE浏览一样 */
   OutputFormat format = OutputFormat.createPrettyPrint();

   /** 指定XML字符集编码 */
   format.setEncoding(&quot;GBK&quot;);
   output = new XMLWriter(new FileWriter(new File(filename)), format);
   output.write(document);
   output.close();
   
   /** 执行成功,需返回1 */
   returnValue = 1;
  } catch (Exception ex) {
   ex.printStackTrace();
  }
  return returnValue;
 }

 public static void main(String[] args) {

  Dom4jDemo temp = new Dom4jDemo();
//  System.out.println(temp.createXMLFile(&quot;f:\\createXML.xml&quot;));
  System.out.println(temp.ModiXMLFile(&quot;f:\\createXML.xml&quot;,
    &quot;f:\\createXML1.xml&quot;));
  System.out.println(temp.formatXMLFile(&quot;f:\\createXML.xml&quot;));

 }

}

```