您好,登錄后才能下訂單哦!
這篇文章給大家介紹怎么在java中使用DOM4J對(duì)XML文件進(jìn)行操作,內(nèi)容非常詳細(xì),感興趣的小伙伴們可以參考借鑒,希望對(duì)大家能有所幫助。
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對(duì)象。 */ Document document = getDocument(); /** * 第二步,修改得到的document對(duì)象 */ /** * 首先,讀取功能 */ List<Book> books = readAllElementsFromXMLDocument(document); traverseBooks(books); /** * 其次,修改功能 修改內(nèi)容:將id為b002的元素的title改為Java Core,Price改為100.01 */ ModifyInformationOfXMLDocument(document); /** * 再者:實(shí)現(xiàn)刪除功能 刪除內(nèi)容:刪除掉id為book1的元素內(nèi)容。 */ deleteInformationOfXMLDocument(document); /** * 最后:實(shí)現(xiàn)添加i新元素功能 添加內(nèi)容:id為book3,title內(nèi)容為:鳳姐玉照,price內(nèi)容為10000.00 */ addNewBookToXMLDocument(document); /** * 第三步:將得到的document對(duì)象持久化保存到硬盤(XML) */ writeToNewXMLDocument(document); } /** * 實(shí)現(xiàn)了添加新節(jié)點(diǎn):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"); } /** * 該方法實(shí)現(xiàn)了使用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中的內(nèi)容 將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); } } /** * 該方法實(shí)現(xiàn)了對(duì)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; } /** * 通過(guò)document對(duì)象將內(nèi)存中的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對(duì)象。 * * @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 + " 價(jià)格為:" + 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>
關(guān)于怎么在java中使用DOM4J對(duì)XML文件進(jìn)行操作就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,可以學(xué)到更多知識(shí)。如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。