XML 的 Dom4j 解析范例,以及 XPath


使用的还是以前的book.xml文档

book.xml

<?xml version="1.0" encoding="UTF-8"?>
<书架>
	<书>
		<书名>Think in Java</书名>
		<作者>海竹</作者>
		<售价>30.0</售价>
	</书>
	<书>
		<书名 ISBN码="521">WEB</书名>
		<作者>西行</作者>
		<售价>29.9</售价>
	</书>
</书架>

看例子:

Dom4jDemo.java

package com.haizhu.xml;

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

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class Dom4jDemo {
	
	public static void read() throws DocumentException{
		// 得到解析器
		SAXReader reader = new SAXReader();
		// 得到 document
		Document document = reader.read(new File("src/com/haizhu/xml/book.xml"));
		// 得到根节点
		Element root = document.getRootElement();
		// 得到根节点的子节点是一个List数组,根据下标取得指定的子节点
		Element book = (Element)root.elements("书").get(1);
		// 取得子节点的内容
		String bookName = book.element("书名").getText();
		// 取得子节点的属性内容
		String attribute = book.element("书名").attributeValue("ISBN码");
		System.out.println(bookName);
		System.out.println(attribute);		
	}
	
	// 给书添加一个 售价
	public static void add() throws DocumentException, IOException{
		// 得到解析器
		SAXReader reader = new SAXReader();
		// 得到 document
		Document document = reader.read(new File("src/com/haizhu/xml/book.xml"));
		// 得到“书”这个节点
		Element book = (Element) document.getRootElement().elements("书").get(1);
		// 添加给书添加一个“售价”
		book.addElement("售价").setText("88.8元");
		
		// 一定不要忘记提交到 XML 文件
		// 另外,之所以输出流转化这么多次,是为了指定编码
		XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream("src/com/haizhu/xml/book.xml"),"UTF-8"));
		writer.write(document);
		writer.close();
	}
	
	
	// 认识 XPath
	// 除了上面导入的  dom4j-1.6.1.jar,还需要  jaxen-1.1-beta-6.jar
	public static void getSelectNode() throws DocumentException, IOException{
		// 得到解析器
		SAXReader reader = new SAXReader();
		// 得到 document
		Document document = reader.read(new File("src/com/haizhu/xml/book.xml"));
		// 得到所有的“书名”节点
		List listName = document.selectNodes("//书名");
		// 得到所有的“书名”且包含“ISBN码”的属性的节点
		List listNameAndAttr = document.selectNodes("//书名[@ISBN码]");
		// 得到第一个“书名”节点
		Node nodeName = document.selectSingleNode("//书名");
		// 得到第一个“书名”且包含“ISBN码”的属性的节点
		Node nodeNameAndAttr = document.selectSingleNode("//书名[@ISBN码]");

		System.out.println(nodeName.getText());
		System.out.println(nodeNameAndAttr.getText());

		// 有一个问题,为什么不能遍历打印呢?而是一个死循环!!!
		Iterator itr = listName.iterator();
		for(int i=0;i<listName.size();i++){
			Element el = (Element)listName.iterator().next();
			System.out.println(el.getName()+":"+el.getText());
		}
		while(itr.hasNext()){
			Element el = (Element)listName.iterator().next();
			System.out.println(el.getName()+":"+el.getText());
		}

		
		for(int i=0;i<listNameAndAttr.size();i++){
			System.out.println(listNameAndAttr.iterator().next());
		}
	}
	public static void main(String[] args) throws DocumentException, IOException{
		getSelectNode();
	}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值