dom4j

在dom4j中运行xpath需要jaxen-1.1-beta-6.jar包


1.XPath 是一门在 XML 文档中查找信息的语言。XPath 用于在 XML 文档中通过元素和属性进行导航。

2.什么是 XPath?

    * XPath 使用路径表达式在 XML 文档中进行导航
    * XPath 包含一个标准函数库
    * XPath 是 XSLT 中的主要元素
    * XPath 是一个 W3C 标准

3.在 XPath 中,有七种类型的节点:元素、属性、文本、命名空间、处理指令、注释以及文档节点(或成为根节点

)。


请看下面这个 XML 文档:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>

</bookstore>

上面的XML文档中的节点例子:

<bookstore> (文档节点)
<author>J K. Rowling</author> (元素节点)
lang="en" (属性节点)

4.SAXReader reader = new SAXReader();
       try {
Document document = reader.read(new File("C:\\test.xml"));
Node root=document.getRootElement();
List<Node> ls=root.selectNodes("*");
System.out.println(ls.size());
for(Node e:ls){
System.out.println(e.getName());
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
以下所有的代码都是用这个类测试的。

5.所有的节点都用Node类来表示;

6.
. 选取当前节点。
.. 选取当前节点的父节点。
例如:
root.selectNodes(".")选择它自己
root.selectNodes("*")选择被调用对象所表示节点下的所有子节点,不包括孙子节点;
root.selectNodes("//*");选择所有文档中的所有节点,不管被调用对象代表哪个节点;
root.selectNodes("/bookstore/book");从根节点开始选择book元素
root.selectNodes("book");现对与当前节点选择book元素
root.selectNodes("//book");选取文档中的book元素,而不管它在哪个位置。
root.selectNodes("//@lang");选取明为lang的所有属性节点
//title[@*] 选取所有带有任意属性的 title 元素。

7.谓语(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。

8.选取若干路径

通过在路径表达式中使用“|”运算符,您可以选取若干个路径
//book/title | //book/price 选取 book 元素的所有 title 和 price 元素。
//title | //price 选取文档中的所有 title 和 price 元素。
/bookstore/book/title | //price 选取属于 bookstore 元素的 book 元素的所有 title 元素,以及文档

中所有的 price 元素。


9.轴

轴可定义相对于当前节点的节点集。
ancestor 选取当前节点的所有先辈(父、祖父等)。
ancestor-or-self 选取当前节点的所有先辈(父、祖父等)以及当前节点本身。
attribute 选取当前节点的所有属性。
child 选取当前节点的所有子元素。
descendant 选取当前节点的所有后代元素(子、孙等)。
descendant-or-self 选取当前节点的所有后代元素(子、孙等)以及当前节点本身。
following 选取文档中当前节点的结束标签之后的所有节点。
namespace 选取当前节点的所有命名空间节点。
parent 选取当前节点的父节点。
preceding 选取文档中当前节点的开始标签之前的所有节点。
preceding-sibling 选取当前节点之前的所有同级节点。
self 选取当前节点。
用法如下:
child::book 选取所有属于当前节点的子元素的 book 节点。
attribute::lang 选取当前节点的 lang 属性。
child::* 选取当前节点的所有子元素。
attribute::* 选取当前节点的所有属性。
child::text() 选取当前节点的所有文本子节点。
child::node() 选取当前节点的所有子节点。
descendant::book 选取当前节点的所有 book 后代。
ancestor::book 选择当前节点的所有 book 先辈。
ancestor-or-self::book 选取当前节点的所有 book 先辈以及当前节点(如果此节点是 book 节点)
child::*/child::price 选取当前节点的所有 price 孙节点。

例如:
下面的例子选取价格高于 35 的所有 title 节点:

/bookstore/book[price>35]/title


含有命名空间的xml文件的解析方法
1.首先创建Xpath对象

改方法时Node接口定义的方法
createXPath

XPath createXPath(String xpathExpression)
                  throws InvalidXPathException
createXPath creates an XPath object for the given xpathExpression. The XPath object allows the

variable context to be specified.

Parameters:
xpathExpression - is the XPath expression to be evaluated
Returns:
an XPath object represeting the given expression
Throws:
InvalidXPathException - if the XPath expression is invalid

Node接口中的方法,只能选择没有任何命名空间的元素;
selectNodes

List<Node> selectNodes(String xpathExpression)
selectNodes evaluates an XPath expression and returns the result as a List of Node instances or String instances depending on the XPath expression.
Parameters:
xpathExpression - is the XPath expression to be evaluated
Returns:
the list of Node or String instances depending on the XPath expression


2.给Xpath对象设定命名空间

XPath是个接口,继承NodeFilter
public interface XPath
extends NodeFilter
NodeFilter是个根接口,没有继承其他任何接口。


下面2个方法是XPath接口中定义的方法
(1)setNamespaceContext
void setNamespaceContext(NamespaceContext namespaceContext)
Sets the namespace context to be used when evaluating XPath expressions
Parameters:
namespaceContext - DOCUMENT ME!

(2)setNamespaceURIs
void setNamespaceURIs(Map<String,String> map)
Sets the current NamespaceContext from a Map where the keys are the String namespace prefixes and the values are the namespace URIs.

For example:

Map uris = new HashMap();
uris.put("SOAP-ENV", "http://schemas.xmlsoap.org/soap/envelope/");
uris.put("m", "urn:xmethodsBabelFish");
XPath xpath = document
         .createXPath("SOAP-ENV:Envelope/SOAP-ENV:Body/m:BabelFish");
xpath.setNamespaceURIs(uris);
Node babelfish = xpath.selectSingleNode(document);

Parameters:
map - the map containing the namespace mappings

3.通过XPath对象选择元素

改方法也是XPath接口中的方法
selectNodes

List<Node> selectNodes(Object context)
selectNodes performs this XPath expression on the given Nodeor Listof Nodes instances appending all the results together into a single list.

Parameters:
context - is either a node or a list of nodes on which to evalute the XPath
Returns:
the results of all the XPath evaluations as a single list

你可能感兴趣的:(dom4j)