私は得る:
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)
objectを使用して Xpath 式を評価しようとしているInputStream
ときに、デバッグを試みましたが、何も問題はありませんでした (もちろん、何かを見逃していました ..) 。コードは次のとおりです。
メインから:
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);
この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
}
補助方法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());
}
どこで間違ったのですか?10倍!