DOM4j解析XML

一XML文件
book.java
package com.itheima.domain;
/**
 * 
* @Decription 书类描述书的信息
 * @author 刘楠
 *
 * @time2016-1-23下午6:19:04
 */
public class Book {
	private String id;//编号
	private String bookname;//书名
	private String author; //作者
	private String price; //价格
	private String  publisher;//出版社
	private String publishDate;//出版时间
	
	
	/*
	 * getter,setter方法
	 */
	public String getId() {
		return id;
	}
	public void setId(String id) {
		this.id = id;
	}
	public String getPublisher() {
		return publisher;
	}
	public void setPublisher(String publisher) {
		this.publisher = publisher;
	}
	public String getBookname() {
		return bookname;
	}
	public void setBookname(String bookname) {
		this.bookname = bookname;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	public String getPublishDate() {
		return publishDate;
	}
	public void setPublishDate(String publishDate) {
		this.publishDate = publishDate;
	}
	/*
	 * toString 方法
	 */
	@Override
	public String toString() {
		return "Book [id=" + id + ", publisher=" + publisher + ", bookname="
				+ bookname + ", author=" + author + ", price=" + price
				+ ", publishDate=" + publishDate + "]";
	}
	

}

books.xml

<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>
<books>
	<book id="001">
		<bookname>Android第一行代码</bookname>
		<author>郭霖</author>
		<price>51.50</price>
		<publisher>人民邮电出版社</publisher>
		<publishDate>2015-06-23</publishDate>
	</book>
	<book id="007">
		<bookname>疯狂的JAVA讲义</bookname>
		<author>李刚</author>
		<price>84.5</price>
		<publisher>电子工业出版社</publisher>
		<publishDate>2012-12-12</publishDate>
	</book>
</books>

二使用DOM4J来解析
2.1解析

	/**
	 * 解析XML
	 * @param tr 文件路径
	 * @throws Exception 
	 */
	public static ArrayList<Book> parseXML(String str) throws Exception{
		
		 ArrayList<Book>  books=new ArrayList<Book>();
		//1.创建解析器,并加载XML文件
		
		SAXReader reader=new SAXReader();
		
		//2.加载XML,到DOM对象中
		Document doc=reader.read(str);
		
		//3.获取根据节点
		Element root=doc.getRootElement();
		
		//4.获取根据节点下的节点集合
		List<Element>elements = root.elements();
		
		//遍历
		for (Element element : elements) {
			Book book=new Book();
			//获取属性
			String id=element.attributeValue("id");
			
			//获取子节点文本内容
			String bookname=element.elementText("bookname");
			String author=element.elementText("author");
			String price=element.elementText("price");
			String publisher=element.elementText("publisher");
			String publishDate=element.elementText("publishDate");
			//为对象设置属性
			book.setId(id);
			book.setAuthor(author);
			book.setBookname(bookname);
			book.setPrice(price);
			book.setPublishDate(publishDate);
			book.setPublisher(publisher);
			//添加到集合
			books.add(book);
			
		}
		return books;
	}


2.2添加 修改与保存
添加子节点
/**
	 * 为XML添加节点
	 * @param str XML文件路径
	 * @throws Exception
	 */
	public static void addElement(String str) throws Exception{
		
		//创建解析器
		SAXReader reader=new SAXReader();
		//加载XML文件到DOM对象
		Document doc=reader.read(str);
		
		//获取XML文件根节点
		Element root = doc.getRootElement();
		//添加一个与已经有的子节点相同的子节点
		//创建新的子节点
		Element element = root.addElement("book");
		//为子节点添加属性与子节点内容
		element.addAttribute("id", "008");
		//创建子节点并添加文本内容
		element.addElement("bookname").setText("跟任何人都聊得来");
		element.addElement("author").setText("美.迈克.贝克特尔");
		element.addElement("price").setText("36.00");
		element.addElement("publisher").setText("九州出版社");
		element.addElement("publishDate").setText("2014-07-01");
		//保存
		saveXml(str,doc);
	}
	/**
	 * 保存XML
	 * @param str  XML路径 
	 * @param doc  DOM对象
	 */
	public static void saveXml(String str,Document doc){
		//设置输出编码
		OutputFormat format=new OutputFormat();
		format.setEncoding("UTF-8");
		//写入XML的对象
		XMLWriter xmlWriter = null;
		try {
			//设置写入流与写入文件的格式
			xmlWriter = new XMLWriter(new FileOutputStream(str), format);
			//写入XML
			xmlWriter.write(doc);
			//提示写入完成
			System.out.println("=====写入完成=====");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				xmlWriter.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
	}



xml文件
<?xml version="1.0" encoding="UTF-8"?>
<books>
	<book id="001">
		<bookname>Android第一行代码</bookname>
		<author>郭霖</author>
		<price>51.50</price>
		<publisher>人民邮电出版社</publisher>
		<publishDate>2015-06-23</publishDate>
	</book>
	<book id="007">
		<bookname>疯狂的JAVA讲义</bookname>
		<author>李刚</author>
		<price>84.5</price>
		<publisher>电子工业出版社</publisher>
		<publishDate>2012-12-12</publishDate>
	</book>
	<book id="008">
		<bookname>跟任何人都聊得来</bookname>
		<author>美.迈克.贝克特尔</author>
		<price>36.00</price>
		<publisher>九州出版社</publisher>
		<publishDate>2014-07-01</publishDate>
	</book>
</books>



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值