3)DOM4J 解析

<?xml version="1.0" encoding="UTF-8"?>

<books> 
  <book id="B1128010001" xx="xixihaha"> 
    <name>Java编程思想(第4版</name>  
    <author>Bruce Eckel</author>  
    <售价>71.9</售价>
    <price>81.0</price>  
    <publish>机械工业出版社</publish> 
  </book>  
  <book> 
    <name>SQL Server 2008 DBA入门经典</name>  
    <author>Chris Leiter</author>  
    <price>63.8</price>  
    <publish>清华大学出版社</publish> 
  </book> 
</books>

—————————————————————————————————————————————————

package cn.zen.dom4j;

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

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

public class Dom4jOperXml {
	/*
	 * 读取第二本书的书名 <name>SQL Server 2008 DBA入门经典</name>
	 */
	@Test
	public void readXml() throws Exception {
		SAXReader sr = new SAXReader();
		// 获取dcument的文档
		Document doc = sr.read(new File("src/book.xml"));

		// 获取文档的根节点
		Element eRoot = doc.getRootElement();

		// 获取到第二本书
		Element eBook2 = (Element) eRoot.elements().get(1);
		// 输出 第二本书的书名
		System.out.println(eBook2.element("name").getText());

		// 获取到第一本书 的属性id值
		Element eBook1 = (Element) eRoot.element("book");
		// 输出 第一本书 的属性id值
		System.out.println(eBook1.attributeValue("id"));// eBook1.attribute("id").getValue();
	}

	@Test
	public void loopXmlElements() throws Exception {
		SAXReader sr = new SAXReader();
		// 获取dcument的文档
		Document doc = sr.read(new File("src/book.xml"));

		// 获取文档的根节点
		Element eRoot = doc.getRootElement();

		// 获取到第二本书
		Element eBook2 = (Element) eRoot.elements().get(1);

		// Iterable
		/*
		 * Iterator it = eBook2.elementIterator(); while (it.hasNext()) { Element e
		 * = (Element) it.next(); System.out.println(e.getText()); }
		 */
		for (Iterator it = eBook2.elementIterator(); it.hasNext();) {
			Element e = (Element) it.next();
			System.out.println(e.getText());
		}
		/*
		 * for (Iterator i = eRoot.element("book").attributeIterator();
		 * i.hasNext();) { Attribute attr = (Attribute) i.next();
		 * System.out.println(attr.getValue()); // do something }
		 */
	}

	/*
	 * 给第一本书添加节点 <售价>71.9</售价>
	 */
	@Test
	public void addElement() throws Exception {
		SAXReader sr = new SAXReader();
		// 获取dcument的文档
		Document doc = sr.read(new File("src/book.xml"));

		// 获取文档的根节点
		Element eRoot = doc.getRootElement();

		Element eBook1 = eRoot.element("book");
		eBook1.addElement("售价").setText("71.9");
		
		OutputFormat of = OutputFormat.createPrettyPrint();
		of.setEncoding("UTF-8");

		XMLWriter xw = new XMLWriter(new FileOutputStream("src/book.xml"), of);
		xw.write(doc);
		xw.close();
	}
	/*
	 * 给第一本书指定位置   添加节点 <售价>71.9</售价>
	 */
	@Test
	public void appointAdd() throws Exception {
		SAXReader sr = new SAXReader();
		// 获取dcument的文档
		Document doc = sr.read(new File("src/book.xml"));

		// 获取文档的根节点
		Element eRoot = doc.getRootElement();

		List eList = eRoot.element("book").elements();
		
		Element ePrice = DocumentHelper.createElement("售价");
		ePrice.setText("71.9");
		
		eList.add(2, ePrice);
		
		OutputFormat of = OutputFormat.createPrettyPrint();
		of.setEncoding("UTF-8");

		XMLWriter xw = new XMLWriter(new FileOutputStream("src/book.xml"), of);
		xw.write(doc);
		xw.close();
	}
	/*
	 * 修改第一本书添加节点 <售价>101.9</售价>
	 */
	@Test
	public void modifyElement() throws Exception {
		SAXReader sr = new SAXReader();
		// 获取dcument的文档
		Document doc = sr.read(new File("src/book.xml"));

		// 获取文档的根节点
		Element eRoot = doc.getRootElement();

		Element eBook1 = eRoot.element("book");
		eBook1.element("售价").setText("101.9");
		
		OutputFormat of = OutputFormat.createPrettyPrint();
		of.setEncoding("UTF-8");

		XMLWriter xw = new XMLWriter(new FileOutputStream("src/book.xml"), of);
		xw.write(doc);
		xw.close();
	}
		
	/*
	 * 删除第一本书  <售价>71.9</售价> 节点
	 */
	@Test
	public void delElement() throws Exception {
		SAXReader sr = new SAXReader();
		// 获取dcument的文档
		Document doc = sr.read(new File("src/book.xml"));

		// 获取文档的根节点
		Element eRoot = doc.getRootElement();

		Element eBook1 = eRoot.element("book");
		eBook1.remove(eBook1.element("售价"));
		
		OutputFormat of = OutputFormat.createPrettyPrint();
		of.setEncoding("UTF-8");

		XMLWriter xw = new XMLWriter(new FileOutputStream("src/book.xml"), of);
		xw.write(doc);
		xw.close();
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值