XPath 中的非法参数异常

发布于 2024-12-18 02:49:27 字数 1346 浏览 0 评论 0原文

每当我运行下面的代码时,如果它找到该单词,它就会给我一个 Illegalgument 异常,但如果没有匹配,它将一直持续到结束,没有错误。有人可以帮我找到解决方案吗?

public static void main(String[] args) throws MalformedURLException, SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException, IOException, SAXException, XPathExpressionException {

   Parser p = new Parser();
   SAX2DOM sax2dom = null;
   org.w3c.dom.Node doc  = null;

   URL url = new URL("http://stackoverflow.com/users/1042952/mostafa");

   p.setFeature(Parser.namespacesFeature, false);
   p.setFeature(Parser.namespacePrefixesFeature, false);
   sax2dom = new SAX2DOM();
   p.setContentHandler(sax2dom);
   p.parse(new InputSource(new InputStreamReader(url.openStream())));
   doc = sax2dom.getDOM();

   final String term = "mostafa";
   String expression = "//*[contains(text(),$term)]";
   final QName termVariableName = new QName("term");
   class TermResolver implements XPathVariableResolver {
      @Override
      public Object resolveVariable(QName variableName) {
         return termVariableName.equals(variableName) ? term : null;
      }
   }
   javax.xml.xpath.XPath xpath = XPathFactory.newInstance().newXPath();
   xpath.setXPathVariableResolver(new TermResolver());
   Node node = (Node) xpath.evaluate(expression, p, termVariableName);
   System.out.println("her is it"+node);
}

Whenever I run the below code, it gives me an Illegalargument exception if it find out the word, but if no matches, it will goes until end with no errors. can anybody help me to find out the solution?

public static void main(String[] args) throws MalformedURLException, SAXNotRecognizedException, SAXNotSupportedException, ParserConfigurationException, IOException, SAXException, XPathExpressionException {

   Parser p = new Parser();
   SAX2DOM sax2dom = null;
   org.w3c.dom.Node doc  = null;

   URL url = new URL("http://stackoverflow.com/users/1042952/mostafa");

   p.setFeature(Parser.namespacesFeature, false);
   p.setFeature(Parser.namespacePrefixesFeature, false);
   sax2dom = new SAX2DOM();
   p.setContentHandler(sax2dom);
   p.parse(new InputSource(new InputStreamReader(url.openStream())));
   doc = sax2dom.getDOM();

   final String term = "mostafa";
   String expression = "//*[contains(text(),$term)]";
   final QName termVariableName = new QName("term");
   class TermResolver implements XPathVariableResolver {
      @Override
      public Object resolveVariable(QName variableName) {
         return termVariableName.equals(variableName) ? term : null;
      }
   }
   javax.xml.xpath.XPath xpath = XPathFactory.newInstance().newXPath();
   xpath.setXPathVariableResolver(new TermResolver());
   Node node = (Node) xpath.evaluate(expression, p, termVariableName);
   System.out.println("her is it"+node);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

瘫痪情歌 2024-12-25 02:49:27

1) 您的直接错误是由于传递给 evaluate 的结果类型非法所致。来自 文档

如果 returnType 不是 XPathConstants 中定义的类型之一(
NUMBER、STRING、BOOLEAN、NODE 或 NODESET),然后是
抛出 IllegalArgumentException。

2) evaluate 的第二个参数应该是上下文节点,而不是解析器。

使用类似这样的内容:

Node node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);

注意:您可能想将 Mostafa 大写。

1) Your immediate error is due to an illegal result type passed to evaluate. From the docs:

If returnType is not one of the types defined in XPathConstants (
NUMBER, STRING, BOOLEAN, NODE or NODESET) then an
IllegalArgumentException is thrown.

2) The second argument to evaluate should be a context node, not the parser.

Use something like this:

Node node = (Node) xpath.evaluate(expression, doc, XPathConstants.NODE);

Note: You probably meant to capitalize Mostafa.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文