public class XPathTool
extends java.lang.Object
All methods take a string XPath specification, along with a context, and produces a resulting java.util.List.
The W3C XPath Specification (http://www.w3.org/TR/xpath) refers to NodeSets repeatedly, but this implementation simply uses java.util.List to hold all Nodes. A 'Node' is any object in a JDOM object tree, such as an org.jdom.Element, org.jdom.Document, or org.jdom.Attribute.
To use it in Velocity, do this:
#set $authors = $xpath.applyTo("document/author", $root)
#foreach ($author in $authors)
$author.getValue()
#end
#set $chapterTitles = $xpath.applyTo("document/chapter/@title", $root)
#foreach ($title in $chapterTitles)
$title.getValue()
#end
In newer Anakia builds, this class is obsoleted in favor of calling
selectNodes() on the element directly:
#set $authors = $root.selectNodes("document/author")
#foreach ($author in $authors)
$author.getValue()
#end
#set $chapterTitles = $root.selectNodes("document/chapter/@title")
#foreach ($title in $chapterTitles)
$title.getValue()
#end
| 构造器和说明 |
|---|
XPathTool()
Constructor does nothing, as this is mostly
just objectified static methods
|
| 限定符和类型 | 方法和说明 |
|---|---|
NodeList |
applyTo(java.lang.String xpathSpec,
org.jdom.Document doc)
Apply an XPath to a JDOM Document
|
NodeList |
applyTo(java.lang.String xpathSpec,
org.jdom.Element elem)
Apply an XPath to a JDOM Element
|
NodeList |
applyTo(java.lang.String xpathSpec,
java.util.List nodeSet)
Apply an XPath to a nodeset
|
public XPathTool()
public NodeList applyTo(java.lang.String xpathSpec, org.jdom.Document doc)
xpathSpec - The XPath to applydoc - The Document contextpublic NodeList applyTo(java.lang.String xpathSpec, org.jdom.Element elem)
xpathSpec - The XPath to applyelem - The Element contextpublic NodeList applyTo(java.lang.String xpathSpec, java.util.List nodeSet)
xpathSpec - The XPath to applynodeSet - The nodeset context