无法使具有函数的xpath与JDOM2一起使用 [英] can not make xpath with functions work with JDOM2
问题描述
我之前使用过JDOM1来解析带有xpath的xml,并且对非通用样式感到厌倦,所以我决定尝试JDOM2,好了,一切对我来说都很完美(通用,XPathFactory,XPathExpression).然后我尝试一个包含功能的xpath语句:
I used JDOM1 before to parse xmls with xpath, and tired with the non-generic style, so I decide to try JDOM2, OK, everything works perfect for me ( the generic, XPathFactory, XPathExpression). then I try a xpath statement with contains function :
XPathExpression<Text> timeXpath = XPathFactory.instance().compile(
"./p[contains(.,'time:')]/text()", Filters.textOnly());
String time = timeXpath.evaluateFirst(div).getTextTrim();
然后我得到了假药:
java.lang.IllegalStateException: Unable to evaluate expression. See cause
at org.jdom2.xpath.jaxen.JaxenCompiled.evaluateRawFirst(JaxenCompiled.java:200)
at org.jdom2.xpath.util.AbstractXPathCompiled.evaluateFirst(AbstractXPathCompiled.java:327)
at peace.org.tm.spider.star.DamaiStarSpider.syncStarTracks(DamaiStarSpider.java:123)
at peace.org.tm.spider.star.DamaiStarSpider.main(DamaiStarSpider.java:156)
Caused by: org.jaxen.UnresolvableException: Function :contains
at org.jaxen.SimpleFunctionContext.getFunction(SimpleFunctionContext.java:142)
at org.jaxen.ContextSupport.getFunction(ContextSupport.java:189)
at org.jaxen.Context.getFunction(Context.java:153)
at org.jaxen.expr.DefaultFunctionCallExpr.evaluate(DefaultFunctionCallExpr.java:183)
at org.jaxen.expr.DefaultPredicate.evaluate(DefaultPredicate.java:106)
at org.jaxen.expr.PredicateSet.evaluatePredicates(PredicateSet.java:188)
at org.jaxen.expr.DefaultLocationPath.evaluate(DefaultLocationPath.java:218)
然后我尝试:
XPathExpression<Text> timeXpath = XPathFactory.instance().compile(
"./p[fn:contains(.,'time:')]/text()", Filters.textOnly());
String time = timeXpath.evaluateFirst(div).getTextTrim();
xpath编译失败:
xpath compile failed:
java.lang.IllegalArgumentException: Unable to compile './p[fn:contains(.,'time:')]/text()'. See Cause.
at org.jdom2.xpath.jaxen.JaxenCompiled.<init>(JaxenCompiled.java:152)
at org.jdom2.xpath.jaxen.JaxenXPathFactory.compile(JaxenXPathFactory.java:82)
at org.jdom2.xpath.XPathFactory.compile(XPathFactory.java:282)
at peace.org.tm.spider.star.DamaiStarSpider.syncStarTracks(DamaiStarSpider.java:91)
at peace.org.tm.spider.star.DamaiStarSpider.main(DamaiStarSpider.java:156)
Caused by: org.jaxen.XPathSyntaxException: Unexpected '('
at org.jaxen.BaseXPath.<init>(BaseXPath.java:136)
at org.jaxen.BaseXPath.<init>(BaseXPath.java:157)
at org.jdom2.xpath.jaxen.JaxenCompiled.<init>(JaxenCompiled.java:150)
... 4 more
我已经在Google堆栈跟踪中搜索了大约2个小时,没有发现有用的信息,我想也许我犯了一个非常愚蠢的错误,有人可以帮我解决这个问题吗?谢谢!
I already google the stack trace for about 2 hours, nothing useful founded, I think maybe I made a very stupid mistake, is anyone can figure it out for me? thanks!
推荐答案
我无法重现正在获取的异常....我将JDOM 2.0.5与Jaxen 1.1.6一起使用.
I can't reproduce the exceptions you are getting.... I am using JDOM 2.0.5 with Jaxen 1.1.6.
我创建了以下内容:
public static void main(String[] args) {
Element root = new Element ("root");
Element p = new Element("p");
p.addContent(" Return this time: Boo! ");
root.addContent(p);
XPathExpression<Text> timeXpath = XPathFactory.instance().compile(
"./p[contains(.,'time:')]/text()", Filters.textOnly());
XPathDiagnostic<Text> xpd = timeXpath.diagnose(root, true);
System.out.println(xpd);
System.out.println(timeXpath.evaluateFirst(root).getTextTrim());
}
它会产生:
[XPathDiagnostic: './p[contains(.,'time:')]/text()' evaluated (first) against org.jdom2.Element produced raw=1 discarded=0 returned=1]
Return this time: Boo!
我相信您可能拥有过时的Jaxen类库?
I believe you may have out-of-date Jaxen class libraries?
这篇关于无法使具有函数的xpath与JDOM2一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!