您的位置:首页 > 其它

dom4j中xpath的使用

2013-09-11 15:58 134 查看
1、获取Document

[java]
view plaincopyprint?

SAXReader saxReader = new SAXReader();

URL url= Thread.currentThread().getContextClassLoader().getResource("beans.xml");

Document document = saxReader.read(url);//参数为File,URL,InputStream,InputSource,Reader等。

SAXReader saxReader = new SAXReader(); 
URL url= Thread.currentThread().getContextClassLoader().getResource("beans.xml");
Document document = saxReader.read(url);//参数为File,URL,InputStream,InputSource,Reader等。

2、查询Element

[java]
view plaincopyprint?

String xpath ="/composites/composite[@type='onDelete']";//查询属性type='ondDelete'的composite


List<Element> composites = document.selectNodes(xpath);

String xpath ="/composites/composite[@type='onDelete']";//查询属性type='ondDelete'的composite 
List<Element> composites = document.selectNodes(xpath);

3、查找带有命名空间的节点 ,有下列xml文件

[html]
view plaincopyprint?

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

<message id="oNVls-26"

to="admin6@172.17.35.3"

from="admin3@172.17.35.3/Smack"

type="chat">

<subject>zscasvgsadg</subject>

<body>sdvgsdvsaddzsfbnzsdfbvdas</body>

<thread>0pMmg6</thread>

<ext xmlns="infoair:obcs:msg:ext">

<identify>1306663476453admin38</identify>

<sendType>NOR</sendType>

<awokeTime>60</awokeTime>

<x xmlns="infoair:obcs:msg:source" var="130666347645334345"/></ext>

</message>

<?xml version="1.0" encoding="UTF-8"?>
<message id="oNVls-26"  
to="admin6@172.17.35.3"  
from="admin3@172.17.35.3/Smack"  
type="chat">   
<subject>zscasvgsadg</subject>   
<body>sdvgsdvsaddzsfbnzsdfbvdas</body>   
<thread>0pMmg6</thread>   
<ext xmlns="infoair:obcs:msg:ext">   
<identify>1306663476453admin38</identify>   
<sendType>NOR</sendType>   
<awokeTime>60</awokeTime>   
<x xmlns="infoair:obcs:msg:source" var="130666347645334345"/></ext>   
</message>

查询 x 元素的 var属性:

[java]
view plaincopyprint?

Map<String, String> names = new HashMap<String, String>();

names.put("a", "infoair:obcs:msg:ext");

names.put("b", "infoair:obcs:msg:source");

XPath xPath = document_createXPath("/message/a:ext/b:x/@var");

xPath.setNamespaceURIs(names);

node = xPath.selectSingleNode(document);

node = xPath.selectSingleNode(document);

String var = node.getText();

Map<String, String> names = new HashMap<String, String>();
        names.put("a", "infoair:obcs:msg:ext");
names.put("b", "infoair:obcs:msg:source");
XPath xPath = document_createXPath("/message/a:ext/b:x/@var");
xPath.setNamespaceURIs(names);
        node = xPath.selectSingleNode(document);
node = xPath.selectSingleNode(document);
String var = node.getText();

4、xpath语法

选取节点

XPath 使用路径表达式在 XML 文档中选取节点。节点是通过沿着路径或者 step 来选取的。

下面列出了最有用的路径表达式:

表达式描述
nodename选取此节点的所有子节点
/从根节点选取
//从匹配选择的当前节点选择文档中的节点,而不考虑它们的位置
.选取当前节点
..选取当前节点的父节点
@选取属性

实例

在下面的表格中,我们已列出了一些路径表达式以及表达式的结果:

路径表达式结果
bookstore选取 bookstore 元素的所有子节点
/bookstore选取根元素 bookstore

注释:假如路径起始于正斜杠( / ),则此路径始终代表到某元素的绝对路径!

bookstore/book选取所有属于 bookstore 的子元素的 book 元素。
//book选取所有 book 子元素,而不管它们在文档中的位置。
bookstore//book选择所有属于 bookstore 元素的后代的 book 元素,而不管它们位于 bookstore 之下的什么位置。
//@lang选取所有名为 lang 的属性。

谓语(Predicates)

谓语用来查找某个特定的节点或者包含某个指定的值的节点。

谓语被嵌在方括号中。

实例

在下面的表格中,我们列出了带有谓语的一些路径表达式,以及表达式的结果:

路径表达式结果
/bookstore/book[1]选取属于 bookstore 子元素的第一个 book 元素。
/bookstore/book[last()]选取属于 bookstore 子元素的最后一个 book 元素。
/bookstore/book[last()-1]选取属于 bookstore 子元素的倒数第二个 book 元素。
/bookstore/book[position()<3]选取最前面的两个属于 bookstore 元素的子元素的 book 元素。
//title[@lang]选取所有拥有名为 lang 的属性的 title 元素。
//title[@lang='eng']选取所有 title 元素,且这些元素拥有值为 eng 的 lang 属性。
/bookstore/book[price>35.00]选取所有 bookstore 元素的 book 元素,且其中的 price 元素的值须大于 35.00。
/bookstore/book[price>35.00]/title选取所有 bookstore 元素中的 book 元素的 title 元素,且其中的 price 元素的值须大于
35.00。

选取未知节点

XPath 通配符可用来选取未知的 XML 元素。

通配符描述
*匹配任何元素节点
@*匹配任何属性节点
node()匹配任何类型的节点

实例

在下面的表格中,我们列出了一些路径表达式,以及这些表达式的结果:

路径表达式结果
/bookstorechild::price选取当前节点的所有 price 孙。

XPath 运算符

下面列出了可用在 XPath 表达式中的运算符:

运算符描述实例返回值
|计算两个节点集//book | //cd返回所有带有 book 和 ck 元素的节点集
+加法6 + 410
-减法6 - 42
*乘法6 * 424
div除法8 div 42
=等于price=9.80如果 price 是 9.80,则返回 true。

如果 price 是 9.90,则返回 fasle。

!=不等于price!=9.80如果 price 是 9.90,则返回 true。

如果 price 是 9.80,则返回 fasle。

<小于price<9.80如果 price 是 9.00,则返回 true。

如果 price 是 9.90,则返回 fasle。

<=小于或等于price<=9.80如果 price 是 9.00,则返回 true。

如果 price 是 9.90,则返回 fasle。

>大于price>9.80如果 price 是 9.90,则返回 true。

如果 price 是 9.80,则返回 fasle。

>=大于或等于price>=9.80如果 price 是 9.90,则返回 true。

如果 price 是 9.70,则返回 fasle。

orprice=9.80 or price=9.70如果 price 是 9.80,则返回 true。

如果 price 是 9.50,则返回 fasle。

andprice>9.00 and price<9.90如果 price 是 9.80,则返回 true。

如果 price 是 8.50,则返回 fasle。

mod计算除法的余数5 mod 2
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: