当前位置: 移动技术网 > IT编程>开发语言>Java > java使用DOM4J对XML文件进行增删改查操作

java使用DOM4J对XML文件进行增删改查操作

2019年07月19日  | 移动技术网IT编程  | 我要评论

本文实例为大家分享了java使用dom4j对xml文件进行增删改查操作的具体代码,供大家参考,具体内容如下

源代码:

dom4j.java

package com.zc.homework19;

import java.io.filewriter;
import java.util.arraylist;
import java.util.iterator;
import java.util.list;

import org.dom4j.document;
import org.dom4j.element;
import org.dom4j.io.saxreader;
import org.dom4j.io.xmlwriter;

import com.zc.homework19.book;

public class dom4j {
  public static void main(string args[]) throws exception {
    /**
     * 第一步,得到document对象。
     */
    document document = getdocument();

    /**
     * 第二步,修改得到的document对象
     */

    /**
     * 首先,读取功能
     */
    list<book> books = readallelementsfromxmldocument(document);
    traversebooks(books);

    /**
     * 其次,修改功能 修改内容:将id为b002的元素的title改为java core,price改为100.01
     */
    modifyinformationofxmldocument(document);

     /**
     * 再者:实现删除功能 删除内容:删除掉id为book1的元素内容。
     */
     deleteinformationofxmldocument(document);

     /**
     * 最后:实现添加i新元素功能 添加内容:id为book3,title内容为:凤姐玉照,price内容为10000.00
     */
     addnewbooktoxmldocument(document);

     /**
     * 第三步:将得到的document对象持久化保存到硬盘(xml)
     */
     writetonewxmldocument(document);
  }

  /**
   * 实现了添加新节点:book的功能
   * 
   * @param document
   */
  private static void addnewbooktoxmldocument(document document) {
    element root = document.getrootelement();
    element newbook = root.addelement("book");
    newbook.addattribute("id", "book3");
    element title = newbook.addelement("title");
    title.settext("凤姐玉照");
    element price = newbook.addelement("price");
    price.settext("10000.01");
  }

  /**
   * 该方法实现了使用dom4j的删除元素的功能
   * 
   * @param document
   */
  private static void deleteinformationofxmldocument(document document) {
    element root = document.getrootelement();
    for (iterator it = root.elementiterator(); it.hasnext();) {
      element book = (element) it.next();
      string id = book.attributevalue("id");
      if ("book1".equals(id)) {
        element parent = book.getparent();
        parent.remove(book);
      }
    }
  }

  /**
   * 该方法的作用是修改document中的内容 将id为b002的元素的title改为java core,price改为100.01
   * 
   * @param document
   */
  private static void modifyinformationofxmldocument(document document) {
    element root = document.getrootelement();
    list books = root.elements();
    for (int i = 0; i < books.size(); i++) {

      element book = (element) books.get(i);
      if ("book2".equals(book.attributevalue("id"))) {

        for (iterator it = book.elementiterator(); it.hasnext();) {
          element node = (element) it.next();
          string type = node.getname();
          if ("title".equals(type)) {
            node.settext("java core");
          }
          if ("price".equals(type)) {
            node.settext("100.01");
          }
        }
      }
    }

    try {
      writetonewxmldocument(document);
    } catch (exception e) {
      // todo auto-generated catch block
      e.printstacktrace();
    }
  }

  /**
   * 遍历集合
   * 
   * @param books
   */
  private static void traversebooks(list<book> books) {
    for (iterator<book> iterator = books.iterator(); iterator.hasnext();) {
      book book = iterator.next();
      system.out.println(book);
    }
  }

  /**
   * 该方法实现了对xml文档的读取功能
   * 
   * @param document
   * @return
   */
  private static list<book> readallelementsfromxmldocument(document document) {
    list<book> books = new arraylist<book>();
    element root = document.getrootelement();
    list list = root.elements();
    for (int i = 0; i < list.size(); i++) {
      element book = (element) list.get(i);
      book b = new book();
      string id = book.attributevalue("id");
      list ll = book.elements();
      b.setid(id);
      system.out.println(id);
      for (int j = 0; j < ll.size(); j++) {
        element element = (element) ll.get(j);
        if ("title".equals(element.getname())) {
          string title = element.gettext();
          b.settitle(title);
          system.out.println(title);
        }
        if ("price".equals(element.getname())) {
          string price = element.gettext();
          double p = double.parsedouble(price);
          b.setprice(p);
          system.out.println(price);
        }
      }
      books.add(b);
    }
    return books;
  }

  /**
   * 通过document对象将内存中的dom树保存到新的xml文档。
   * 
   * @param document
   * @throws exception
   */
  private static void writetonewxmldocument(document document)
      throws exception {

    xmlwriter writer = new xmlwriter(new filewriter(
        "src/com/zc/homework19/newbooks.xml"));
    writer.write(document);
    writer.close();
  }

  /**
   * 该方法用于得到document对象。
   * 
   * @return
   * @throws exception
   */
  private static document getdocument() throws exception {
    saxreader sr = new saxreader();
    document document = sr.read("src\\books.xml");
    return document;
  }
}

book.java

package com.zc.homework19;

public class book {
  public string title;
  public double price;
  public string id;

  public string gettitle() {
    return title;
  }

  public void settitle(string title) {
    this.title = title;
  }

  public double getprice() {
    return price;
  }

  public void setprice(double price) {
    this.price = price;
  }

  public string getid() {
    return id;
  }

  public void setid(string id) {
    this.id = id;
  }

  public string tostring() {
    return "图书isbn为:" + id + "  书名为:" + title + "  价格为:" + price;
  }
}

book.xml

<?xml version="1.0" encoding="utf-8"?>
<books>
  <book id="book1">
    <title>java编程思想</title>
    <price>80.00</price>
  </book>
  <book id="book2">
    <title>java 编程理论</title>
    <price>100.00</price>
  </book>
</books>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网