0

我之前使用 JDOM1 用 xpath 解析 xml,并且厌倦了非泛型样式,所以我决定尝试 JDOM2,好吧,一切对我来说都很完美(泛型、XPathFactory、XPathExpression)。然后我尝试使用包含函数的 xpath 语句:

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 编译失败:

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

我已经在谷歌上搜索了大约 2 个小时的堆栈跟踪,没有发现任何有用的信息,我想也许我犯了一个非常愚蠢的错误,有人能帮我弄清楚吗?谢谢!

4

1 回答 1

0

我无法重现您遇到的异常......我正在使用 JDOM 2.0.5 和 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 类库?

于 2013-10-28T14:16:15.350 回答