Java DOM4J 方式解析XML檔案

猿來緣去發表於2017-04-25

Dom4J方式解析XML檔案。dom4j是非官方提供的xml檔案解析方式,因此需要去第三方下載dom4j的jar包

books.xml

<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
	<book id="1">
		<name>懂得生活</name>
		<author>Mr Azzan</author>
		<year>2017</year>
		<price>100.00</price>
	</book>
	<book id="2">
		<name>熱愛生活</name>
		<author>Miss Sun</author>
		<year>2017</year>
		<price>121.00</price>
	</book>
	<book id="3">
		<name>品味生活</name>
		<author>Miss Sun</author>
		<year>2016</year>
		<price>111.00</price>
	</book>
	<book id="4">
		<name>珍惜生活</name>
		<author>Miss Azzan</author>
		<year>2014</year>
		<price>110.00</price>
	</book>
</bookstore>
Book.java

package bookentity;

/**
 * book實體
 */
public class Book {
	private String id;
	private String name;
	private String author;
	private String year;
	private String price;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getAuthor() {
		return author;
	}

	public void setAuthor(String author) {
		this.author = author;
	}

	public String getYear() {
		return year;
	}

	public void setYear(String year) {
		this.year = year;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}
}
Dom4jTest.java

package Dom4jReadXMLFile;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;

import bookentity.Book;

public class Dom4jTest {
	// 接收書籍實體的集合
	private static ArrayList<Book> bookList = new ArrayList<Book>();

	public static ArrayList<Book> getBookList() {
		return bookList;
	}

	private void dom4jReadXMLFile() {
		// Dom4j解析books.xml
		// 建立的物件reader
		SAXReader reader = new SAXReader();
		try {
			// 通過reader物件的read方法載入books.xml檔案,獲取document物件
			Document document = reader.read(new File("books.xml"));
			// 通過document物件獲取根節點bookstore
			Element bookstore = document.getRootElement();
			// 通過element物件的elementIterator方法獲取迭代器
			Iterator it = bookstore.elementIterator();
			// 全域性變數記錄第幾本書籍
			int num = 0;
			// 遍歷迭代器,獲取根節點中的資訊(書籍)
			while (it.hasNext()) {
				Book bookEntity = new Book();
				num++;
				System.out.println("====開始遍歷" + num + "本書====");
				Element book = (Element) it.next();
				// 獲取book的屬性名以及屬性值
				List<Attribute> bookAttrs = book.attributes();
				for (Attribute attr : bookAttrs) {
					System.out.println("屬性名:" + attr.getName() + "--屬性值:"
							+ attr.getStringValue());
					bookEntity.setId(attr.getStringValue());
				}
				// 解析子節點的資訊
				Iterator itt = book.elementIterator();
				while (itt.hasNext()) {
					Element bookChild = (Element) itt.next();
					System.out.println("節點名:" + bookChild.getName() + "--節點值:"
							+ bookChild.getStringValue());

					if (bookChild.getName().equals("name")) {
						bookEntity.setName(bookChild.getStringValue());
					}
					if (bookChild.getName().equals("author")) {
						bookEntity.setAuthor(bookChild.getStringValue());
					}
					if (bookChild.getName().equals("year")) {
						bookEntity.setYear(bookChild.getStringValue());
					}
					if (bookChild.getName().equals("price")) {
						bookEntity.setPrice(bookChild.getStringValue());
					}
				}
				// 將書籍存入書籍集合中
				bookList.add(bookEntity);
				// 將書籍實體設定為null,節省資源
				bookEntity = null;
				System.out.println("====結束遍歷" + num + "本書====");
				System.out.println();//換行
			}
		} catch (DocumentException e) {
			e.printStackTrace();
		}

		// 驗證照籍集合中是否成功存入書籍
		for (Book b : bookList) {
			System.out.println("++++開始++++");
			System.out.println(b.getId());
			System.out.println(b.getName());
			System.out.println(b.getAuthor());
			System.out.println(b.getYear());
			System.out.println(b.getPrice());
			System.out.println("++++結束++++");
			System.out.println();//換行
		}
	}
	

	@Test
	public void test() {
		dom4jReadXMLFile();
	}


}
執行結果截圖:


相關文章