title: runtime exception for xpath
body: <p>i have a function which in this function, i use Xpath to get the position of a node as below:</p>
  
  <pre><code>   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;   
  }
  </code></pre>
  
  <p>basically i use this to get a node of a DOM which made from a URL html code, but i face two problems with it, firstly, sometimes i get this exception:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>and also this one for some other kind of nodes:</p>
  
  <pre><code>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.&lt;init&gt;(XPath.java:176)
  at com.sun.org.apache.xpath.internal.XPath.&lt;init&gt;(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
  </code></pre>
  
  <p>but the funny thing is, in booth cases, the nodes are a <code>[#text]</code> node which made me confused why it happens.</p>