0

我有一个函数,在这个函数中,我使用 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

1 回答 1

0

我制作了一个函数,可以根据需要返回节点的位置:

测试.xml

    <html>
           <div id='teste'>Teste</div>
           <div id='poutine'>poutine</div>
           <div id='ola'>Ola tudo ebm!</div>
   </html>

XMLManager 类:

           public final class XMLManager {


            public static Integer getPositionByNode(Node node, File filteForLookUp){

                Integer position = null;    

                try {

                    Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(filteForLookUp);

                    NodeList ndList = doc.getElementsByTagName("*");


                    if(ndList != null){

                        for(int i=0;i<ndList.getLength();i++){

                            if(ndList.item(i).isEqualNode(node)){
                                position = i;
                                System.out.println("Dans la condition");
                            }


                        }


                    }

                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                return position;

            }

        }

我的主要:

            public class MyMain {

            /**
             * @param args
             */
            public static void main(String[] args) {


                 Document doc = null;
                 File file = new File("D:\\Loic_Workspace\\Test2\\res\\test.xml");
                try {
                    doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(file);
                    System.out.println(doc.getElementsByTagName("*").item(2).getTextContent());
                    System.out.println(XMLManager.getPositionByNode(doc.getElementsByTagName("*").item(2), file));
                } catch (SAXException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }



            }

        }

控制台中的输出:

poutine Dans la 条件 2

顺便说一句,获取 [#text] 并不奇怪,你无法通过 getTextContent() 方法获取值。


希望它有帮助;)

于 2012-12-06T00:33:14.913 回答