您的位置:首页 > 其它

XML 的 Dom4j 解析范例,以及 XPath

2013-08-22 23:34 357 查看
使用的还是以前的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();
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: