本文实例为大家分享了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 = getdocument();
List books = readAllElementsFromXMLdocument(document);
traverseBooks(books);
ModifyInformationOfXMLdocument(document);
deleteInformationOfXMLdocument(document);
addNewBookToXMLdocument(document);
writeTonewXMLdocument(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");
}
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);
}
}
}
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();
}
}
private static void traverseBooks(List books) {
for (Iterator iterator = books.iterator(); iterator.hasNext();) {
Book book = iterator.next();
System.out.println(book);
}
}
private static List readAllElementsFromXMLdocument(document document) {
List books = new ArrayList();
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;
}
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();
}
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
JAVA编程思想 80.00 JAVA 编程理论 100.00
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持考高分网。



