public interface XPath
javax.xml.xpath.XPath |
XPath
提供对XPath评估环境和表达式的访问。
Evaluation of XPath Expressions. | |
---|---|
context | If a request is made to evaluate the expression in the absence of a context item, an empty document node will be used for the context. For the purposes of evaluating XPath expressions, a DocumentFragment is treated like a Document node. |
variables | If the expression contains a variable reference, its value will be found through the XPathVariableResolver set with setXPathVariableResolver(XPathVariableResolver) . An XPathExpressionException is raised if the variable resolver is undefined or the resolver returns null for the variable. The value of a variable must be immutable through the course of any single evaluation. |
functions | If the expression contains a function reference, the function will be found through the XPathFunctionResolver set with setXPathFunctionResolver(XPathFunctionResolver) . An XPathExpressionException is raised if the function resolver is undefined or the function resolver returns null for the function. |
QNames | QNames in the expression are resolved against the XPath namespace context set with setNamespaceContext(NamespaceContext) . |
result | This result of evaluating an expression is converted to an instance of the desired return type. Valid return types are defined in XPathConstants . Conversion to the return type follows XPath conversion rules. |
Public methods |
|
---|---|
abstract XPathExpression |
compile(String expression) 编译XPath表达式以供以后评估。 |
abstract Object |
evaluate(String expression, Object item, QName returnType) 在指定的上下文中评估 |
abstract String |
evaluate(String expression, Object item) 在指定的上下文中评估XPath表达式并将结果作为 |
abstract Object |
evaluate(String expression, InputSource source, QName returnType) 在指定的 |
abstract String |
evaluate(String expression, InputSource source) 在指定的 |
abstract NamespaceContext |
getNamespaceContext() 返回当前名称空间上下文。 |
abstract XPathFunctionResolver |
getXPathFunctionResolver() 返回当前函数解析器。 |
abstract XPathVariableResolver |
getXPathVariableResolver() 返回当前的变量解析器。 |
abstract void |
reset() 将此 |
abstract void |
setNamespaceContext(NamespaceContext nsContext) 建立一个名称空间上下文。 |
abstract void |
setXPathFunctionResolver(XPathFunctionResolver resolver) 建立一个功能解析器。 |
abstract void |
setXPathVariableResolver(XPathVariableResolver resolver) 建立一个变量解析器。 |
XPathExpression compile (String expression)
编译XPath表达式以供以后评估。
如果expression
包含任何XPathFunction
,则它们必须通过XPathFunctionResolver
。 一个XPathExpressionException
将被抛出,如果XPathFunction
不能与解决XPathFunctionResolver
。
如果 expression
是 null
,则引发 NullPointerException
。
Parameters | |
---|---|
expression |
String : The XPath expression. |
Returns | |
---|---|
XPathExpression |
Compiled XPath expression. |
Throws | |
---|---|
XPathExpressionException |
If expression cannot be compiled. |
NullPointerException |
If expression is null . |
Object evaluate (String expression, Object item, QName returnType)
在指定的上下文中评估 XPath
表达式,并将结果作为指定的类型返回。
有关上下文项评估,变量,函数和 QName
分辨率和返回类型转换,请参见 Evaluation of XPath Expressions 。
如果 returnType
不在定义的类型的一个 XPathConstants
( NUMBER
, STRING
, BOOLEAN
, NODE
或 NODESET
)那么 IllegalArgumentException
被抛出。
如果null
提供了一种用于值item
,一个空文档将被用于的上下文。 如果expression
或returnType
是null
,则引发NullPointerException
。
Parameters | |
---|---|
expression |
String : The XPath expression. |
item |
Object : The starting context (node or node list, for example). |
returnType |
QName : The desired return type. |
Returns | |
---|---|
Object |
Result of evaluating an XPath expression as an Object of returnType . |
Throws | |
---|---|
XPathExpressionException |
If expression cannot be evaluated. |
IllegalArgumentException |
If returnType is not one of the types defined in XPathConstants . |
NullPointerException |
If expression or returnType is null . |
String evaluate (String expression, Object item)
在指定的上下文中评估XPath表达式,并将结果作为 String
返回。
此方法调用 evaluate(String, Object, QName)
具有 returnType
的 STRING
。
有关上下文项评估,变量,函数和QName解析以及返回类型转换,请参见 Evaluation of XPath Expressions 。
如果null
提供了一种用于值item
,一个空文档将被用于的上下文。 如果expression
是null
,则引发NullPointerException
。
Parameters | |
---|---|
expression |
String : The XPath expression. |
item |
Object : The starting context (node or node list, for example). |
Returns | |
---|---|
String |
The String that is the result of evaluating the expression and converting the result to a String . |
Throws | |
---|---|
XPathExpressionException |
If expression cannot be evaluated. |
NullPointerException |
If expression is null . |
Object evaluate (String expression, InputSource source, QName returnType)
在指定的 InputSource
的上下文中评估XPath表达式,并将结果作为指定的类型返回。
此方法为 InputSource
构建数据模型,并在生成的文档对象上调用 evaluate(String, Object, QName)
。
有关上下文项评估,变量,函数和QName解析以及返回类型转换,请参见 Evaluation of XPath Expressions 。
如果 returnType
不在定义的类型之一 XPathConstants
,那么 IllegalArgumentException
被抛出。
如果 expression
, source
或者 returnType
为 null
,则 NullPointerException
被抛出。
Parameters | |
---|---|
expression |
String : The XPath expression. |
source |
InputSource : The input source of the document to evaluate over. |
returnType |
QName : The desired return type. |
Returns | |
---|---|
Object |
The Object that encapsulates the result of evaluating the expression. |
Throws | |
---|---|
XPathExpressionException |
If expression cannot be evaluated. |
IllegalArgumentException |
If returnType is not one of the types defined in XPathConstants . |
NullPointerException |
If expression , source or returnType is null . |
String evaluate (String expression, InputSource source)
在指定的 InputSource
的上下文中评估XPath表达式,并将结果返回为 String
。
此方法调用 evaluate(String, InputSource, QName)
具有 returnType
的 STRING
。
有关上下文项评估,变量,函数和QName解析以及返回类型转换,请参见 Evaluation of XPath Expressions 。
如果 expression
或 source
是 null
,则引发 NullPointerException
。
Parameters | |
---|---|
expression |
String : The XPath expression. |
source |
InputSource : The InputSource of the document to evaluate over. |
Returns | |
---|---|
String |
The String that is the result of evaluating the expression and converting the result to a String . |
Throws | |
---|---|
XPathExpressionException |
If expression cannot be evaluated. |
NullPointerException |
If expression or source is null . |
NamespaceContext getNamespaceContext ()
返回当前名称空间上下文。
null
在没有名称空间上下文有效的情况下返回。
Returns | |
---|---|
NamespaceContext |
Current Namespace context. |
XPathFunctionResolver getXPathFunctionResolver ()
返回当前函数解析器。
null
在没有函数解析器生效的情况下返回。
Returns | |
---|---|
XPathFunctionResolver |
Current function resolver. |
XPathVariableResolver getXPathVariableResolver ()
返回当前的变量解析器。
null
在没有变量解析器有效时返回。
Returns | |
---|---|
XPathVariableResolver |
Current variable resolver. |
void reset ()
将此 XPath
重置为其原始配置。
XPath
被重置为与使用newXPath()
创建时相同的状态。 reset()
旨在允许重复使用现有的XPath
从而节省与创建新的XPath
相关的资源。
复位XPath
不能保证具有相同XPathFunctionResolver
, XPathVariableResolver
或者NamespaceContext
Object
S,如equals(Object)
。 但保证具有功能相等XPathFunctionResolver
, XPathVariableResolver
和NamespaceContext
。
void setNamespaceContext (NamespaceContext nsContext)
建立一个名称空间上下文。
一个 NullPointerException
如果抛出 nsContext
为 null
。
Parameters | |
---|---|
nsContext |
NamespaceContext : Namespace context to use. |
Throws | |
---|---|
NullPointerException |
If nsContext is null . |
void setXPathFunctionResolver (XPathFunctionResolver resolver)
建立一个功能解析器。
一个 NullPointerException
如果抛出 resolver
为 null
。
Parameters | |
---|---|
resolver |
XPathFunctionResolver : XPath function resolver. |
Throws | |
---|---|
NullPointerException |
If resolver is null . |
void setXPathVariableResolver (XPathVariableResolver resolver)
建立一个变量解析器。
一个 NullPointerException
如果抛出 resolver
为 null
。
Parameters | |
---|---|
resolver |
XPathVariableResolver : Variable resolver. |
Throws | |
---|---|
NullPointerException |
If resolver is null . |