简体   繁体   English

评估Xpath时InputStream异常

[英]InputStream exception while evaluating Xpath

I get : javax.xml.transform.TransformerException: Unable to evaluate expression using this context 我得到: javax.xml.transform.TransformerException: Unable to evaluate expression using this context

    at com.sun.org.apache.xpath.internal.XPath.execute(Unknown Source)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.eval(Unknown Source)
    at com.sun.org.apache.xpath.internal.jaxp.XPathImpl.evaluate(Unknown Source)
    at XPathImplementation.evaluate(XPathImplementation.java:136)
    at Main.main(Main.java:23)
Caused by: java.lang.RuntimeException: Unable to evaluate expression using this context
    at com.sun.org.apache.xpath.internal.axes.NodeSequence.setRoot(Unknown Source)
    at com.sun.org.apache.xpath.internal.axes.LocPathIterator.execute(Unknown Source)

While trying to evaluate an Xpath expression using InputStream object , I've tried to debug it however found nothing wrong (and of course I missed something ..) . 在尝试使用InputStream对象评估Xpath表达式时,我尝试调试它,但是没有发现任何错误(当然,我也错过了什么..)。 Here's the code: 这是代码:

From Main: 从主要:

    XPathProject m = new XPathImplementation();
    m.loadXML("books.xml"); 
    String q = "inventory/book/chapter[3]/preceding-sibling::chapter//title";
    Object ob = m.evaluate(q, null, XPathConstants.NODESET);

We use this evaluate method : 我们使用这种evaluate方法:

public Object evaluate(String expression, Node source, QName returnType) throws XPathExpressionException,IllegalArgumentException,NullPointerException, TransformerException
{
...
  InputStream  is = nodeToInputStream(source);
  Object returnedObject= xpath.evaluate(expression, is, returnType); // it happens here !!

... more code
}

Auxiliary method nodeToInputStream : 辅助方法nodeToInputStream

/*
 *    Convert Node object into InputStream object
 */

    private InputStream nodeToInputStream(Node node) throws TransformerException {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        StreamResult outputTarget = new StreamResult(outputStream);
        Transformer t = TransformerFactory.newInstance().newTransformer();
        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        t.transform(new DOMSource(node), outputTarget);
        return new ByteArrayInputStream(outputStream.toByteArray());
}

Any idea where did I go wrong ? 知道我哪里出错了吗? 10x! 10倍!

Well m.evaluate(q, null, XPathConstants.NODESET); m.evaluate(q, null, XPathConstants.NODESET); passes in a null reference to the evaluate method so you create new DOMSource(null) I think which does not seem to make sense to me and probably results in that error later where a relative XPath inventory/book/chapter[3]/preceding-sibling::chapter//title is evaluated. 将null引用传递给评估方法,因此您创建了new DOMSource(null)我认为这对我而言似乎没有意义,并且可能会在稍后导致相对于XPath inventory/book/chapter[3]/preceding-sibling::chapter//title被评估。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM