Java DOM4J 方式解析XML文件

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

books.xml



	
		懂得生活
		Mr Azzan
		2017
		100.00
	
	
		热爱生活
		Miss Sun
		2017
		121.00
	
	
		品味生活
		Miss Sun
		2016
		111.00
	
	
		珍惜生活
		Miss Azzan
		2014
		110.00
	
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 bookList = new ArrayList();

	public static ArrayList 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 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();
	}


}
运行结果截图:

Java DOM4J 方式解析XML文件_第1张图片

你可能感兴趣的:(JavaSE,java)