java之使用dom4j来实现对xml文件进行解析操作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_34970891/article/details/80785157
<?xml version="1.0" encoding="UTF-8"?>
<bookStore>
	<book id="1">
		<name>
			冰与火之歌
		</name>
		<year>
			2018年6月22日
		</year>
		<price>
			¥50.00
		</price>
		<language>
			Chinese
		</language>
		<author>
			乔治马丁
		</author>
	</book>
	<book id="2">
		<name>
			诸神的黄昏
		</name>
		<year>
			2018年7月22日
		</year>
		<price>
			¥70.00
		</price>
		<language>
			English
		</language>
		<author>
			普罗米修斯
		</author>
	</book>
</bookStore>
package javaxml;
//建立一个与xml文件所对应的book类实例化对象
public class Book {
	private String id;
	private String name;
	private String author;
	private String year;
	private String price;
	private String language;
	public Book() {
		// TODO Auto-generated constructor stub
	}
	public Book(String id, String name, String author, String year,
			String price, String language) {
		super();
		this.id = id;
		this.name = name;
		this.author = author;
		this.year = year;
		this.price = price;
		this.language = language;
	}
	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;
	}
	public String getLanguage() {
		return language;
	}
	public void setLanguage(String language) {
		this.language = language;
	}
	/* (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return "Book [id=" + id + ", name=" + name + ", author=" + author
				+ ", year=" + year + ", price=" + price + ", language="
				+ language + "]";
	}
	
}
package javaxml;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class DOM4JUtil {
//	对xml文件进行解析
	public List<Book> parseUtil() throws Exception
	{
		List<Book> books=new ArrayList<>();
		Book book=null;
//		实例化一个解析器对象
		SAXReader saxReader=new SAXReader();
		String path=DOM4JUtil.class.getResource("/").toString();
		System.out.println("path="+path);
		String realPath=path.substring(path.indexOf("/")+1)+"book.xml";
		System.out.println(realPath);
		File file=new File(realPath);
//		将xml文件对象读入到输入流当中去
		InputStream inputStream=new FileInputStream(file);
//		将输入流对象当中所读取到的xml文件数据转换为一个document对象
		Document document=saxReader.read(inputStream);
//		进行根结点对象的获取操作
		Element	root=document.getRootElement();
		System.out.println("根结点的名字为:"+root.getName());
//		建立一个迭代器对象用于实现对根结点对象当中的子结点进行遍历操作
		Iterator listIterator=root.elementIterator();
		while(listIterator.hasNext())
		{
			Element bookNode=(Element)listIterator.next();
//			System.out.println("子结点:"+bookNode.getName());
//			开始对book子节点对象当中的数据进行获取
			book=new Book();
//			对book标签对象当中属性值进行获取操作,并以集合的形式进行返回
			List<Attribute> attributes=bookNode.attributes();
//			对当前book标签当中的属性进行遍历输出
			for (Attribute attribute : attributes)
			{
				System.out.println("attributeName="+attribute.getName()+",attributeValue="+attribute.getValue());
				if(attribute.getName().equals("id"))
					book.setId(attribute.getValue());
			}
				//对book标签对象当中的子标签对象进行遍历操作
				Iterator iteratorBook=bookNode.elementIterator();
				while(iteratorBook.hasNext())
				{
					Element childNode=(Element) iteratorBook.next();
					System.out.println("子结点为:"+childNode.getName());
					if(childNode.getName().equals("name"))
					{
						System.out.println(childNode.getTextTrim());
						book.setName(childNode.getTextTrim());
					}
					if(childNode.getName().equals("year"))
					{
						System.out.println(childNode.getTextTrim());
						book.setYear(childNode.getTextTrim());
					}
					if(childNode.getName().equals("price"))
					{
						System.out.println(childNode.getTextTrim());
						book.setPrice(childNode.getTextTrim());
					}
					if(childNode.getName().equals("language"))
					{
						System.out.println(childNode.getTextTrim());
						book.setLanguage(childNode.getTextTrim());
					}
					if(childNode.getName().equals("author"))
					{
						System.out.println(childNode.getTextTrim());
						book.setAuthor(childNode.getTextTrim());
					}
				}
			books.add(book);
		}
		inputStream.close();
		return books;
	}
	public static void main(String[] args)
	{
		List<Book> books=new ArrayList<>();
		DOM4JUtil dom4jUtil=new DOM4JUtil();
		try {
			books=dom4jUtil.parseUtil();
			for (Book book : books) {
				System.out.println(book);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}



展开阅读全文

没有更多推荐了,返回首页