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

网友投稿 290 2023-01-16


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

本文实例为大家分享了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();

/**

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

*/

/**

* 首先,读取功能

*/

List 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)hOQNTZW;

}

/**

* 实现了添加新节点: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 books) {

for (Iterator iterator = books.iterator(); iterator.hasNext();) {

Book book = iterator.next();http://

System.out.println(book);

}

}

/**

* 该方法实现了对xml文档的读取功能

*

* @param document

* @return

*/

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;

}

/**

* 通过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

80.00

100.00


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java的单例设计模式详解
下一篇:浅谈基于Token的WEB后台认证机制
相关文章

 发表评论

暂时没有评论,来抢沙发吧~