1

我有一个函数,在这个函数中,我使用 Xpath 来获取节点的位置,如下所示:

   Node goTo;
.....


private Node xpathgo(Node node) throws XPathExpressionException {
       XPath xpath = XPathFactory.newInstance().newXPath();
       Node result;

 if (node == null || node.getNodeName() == null){
            return null;
        }

               xpathgo(node.getFirstChild());


               result =   (Node) xpath.evaluate("//*[. = \"" + goTo.getNodeValue() + "\"]", node,XPathConstants.NODE);


            xpathgo(node.getNextSibling());
 return  result;   
}

基本上我用它来获取一个由 URL html 代码组成的 DOM 节点,但我面临两个问题,首先,有时我得到这个异常:

Exception in thread "main" java.lang.RuntimeException: Could not resolve the node to a handle
    at com.sun.org.apache.xml.internal.dtm.ref.DTMManagerDefault.getDTMHandleFromNode(DTMManagerDefault.java:574)
    at com.sun.org.apache.xpath.internal.XPathContext.getDTMHandleFromNode(XPathContext.java:182)
    at com.sun.org.apache.xpath.internal.XPath.execute(XPath.java:301)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:210)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)

还有这个用于其他类型的节点:

Caused by: javax.xml.transform.TransformerException: Expected ], but found: the
at com.sun.org.apache.xpath.internal.compiler.XPathParser.error(XPathParser.java:608)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.consumeExpected(XPathParser.java:526)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.Predicate(XPathParser.java:1935)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.Step(XPathParser.java:1724)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelativeLocationPath(XPathParser.java:1624)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.LocationPath(XPathParser.java:1595)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.PathExpr(XPathParser.java:1315)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnionExpr(XPathParser.java:1234)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.UnaryExpr(XPathParser.java:1140)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.MultiplicativeExpr(XPathParser.java:1061)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.AdditiveExpr(XPathParser.java:1003)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.RelationalExpr(XPathParser.java:928)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.EqualityExpr(XPathParser.java:868)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.AndExpr(XPathParser.java:832)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.OrExpr(XPathParser.java:805)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.Expr(XPathParser.java:788)
at com.sun.org.apache.xpath.internal.compiler.XPathParser.initXPath(XPathParser.java:127)
at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:176)
at com.sun.org.apache.xpath.internal.XPath.<init>(XPath.java:264)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(XPathImpl.java:193)
at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(XPathImpl.java:275)
... 7 more

但有趣的是,在展位案例中,节点是一个[#text]让我困惑为什么会发生的节点。

4

0 回答 0