dom4j中XPath用法

大家经常运用DOM4J操作XML文档,如果XML文档存在namespace的话,读写XML经常不工作。下面是一个简单的例子,例子中需要用到的两个xml文件:a.xml与book.xml见附件。代码如下:
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;


public class XpathTest {
/**
* 已知命名空间,获得该命名空间下节点
* @throws Exception
*/
public static void test1() throws Exception {
SAXReader reader = new SAXReader();
String uri = "http://WebXml.com.cn/";//命名空间
HashMap map = new HashMap();//将uri存入map中
map.put("xx", uri);
reader.getDocumentFactory().setXPathNamespaceURIs(map);

InputStream is = new FileInputStream("src/a.xml");
org.dom4j.Document document = reader.read(is);

org.dom4j.XPath xpath = DocumentHelper.createXPath("//xx:string");

List list = xpath.selectNodes(document);//等价List list = document.selectNodes("//xx:string");
for(int i=0; i<list.size(); i++){
Node node = (Node)list.get(i);//转型为Node
System.out.println("hah====="+node.getText());

Element e = (Element)list.get(i);//转型为Element
System.out.println(e.getText());
}
}

/**
* 先通过document获得命名空间,再获得该命名空间下节点
* @throws Exception
*/
public static void test2() throws Exception {
SAXReader reader = new SAXReader();
InputStream is = new FileInputStream("src/a.xml");
org.dom4j.Document document = reader.read(is);

//获得节点的命名空间
String uri = document.getRootElement().element("Body")
.element("getWeatherbyCityNameResponse").getNamespaceURI();
//将uri存入map中
HashMap map = new HashMap();
map.put("xx", uri);

org.dom4j.XPath xpath = DocumentHelper.createXPath("//xx:string");
xpath.setNamespaceURIs(map);

List list = xpath.selectNodes(document);
for(int i=0; i<list.size(); i++){
Node node = (Node)list.get(i);//转型为Node
System.out.println("hah====="+node.getText());

Element e = (Element)list.get(i);//转型为Element
System.out.println(e.getText());
}
}

/**
* 运用jdk中自带包javax.xml.xpath包
* @throws Exception
*/
public static void test3() throws Exception {
//将XML文档加载到DOM Document对象中
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = factory.newDocumentBuilder();
org.w3c.dom.Document doc = builder.parse("src/books.xml");
//创建 XPathFactory
XPathFactory pathFactory = XPathFactory.newInstance();
//使用XPathFactory工厂创建 XPath 对象
XPath xpath = pathFactory.newXPath();
//使用XPath对象编译XPath表达式
XPathExpression pathExpression = xpath
.compile("//book[author='TEST']/title/text()");
//计算 XPath 表达式得到结果
Object result = pathExpression.evaluate(doc, XPathConstants.NODESET);
//节点集node-set转化为NodeList
//将结果强制转化成 DOM NodeList
org.w3c.dom.NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
//查找所有图书的 XPath 查询非常简单:
//book[author="TEST"]
//book代表节点的名称,author属性的名称,后面是要查询的值

//为了找出这些图书的标题(title),只要增加一步,表达式就变成了:
//book[author="TEST"]/title
//title代表要取元素的名称

//最后,真正需要的是 title 元素的文本节点内容.这就要求再增加一步,完整的表达式就是:
//book[author="TEST"]/title/text()
//text()该节点的内容
}

public static void main(String[] args) throws Exception{
test1();
//test2();
//test3();
}
}

以下是几个XPath讲解的资料:
dom4j中XPath的使用:[url]http://nieli.iteye.com/blog/540720[/url]
xpath的数据和节点类型以及XPath中节点匹配的基本方法:[url]http://www.iteye.com/topic/258109[/url]
编写简单的XPath程序:[url]http://www.iteye.com/topic/153252[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值