我正在使用 apache JXPath 库来解析 XML。我试图在 JXPath 中找到一个与 XPath 评估类似的功能的 API,即检查 xpath 表达式是否存在?它的相似之处在于
<xsl:when test="
使用 xslt 时。使用 XPath 我可以类似地做
XPathFactory xpathFactory = XPathFactory.newInstance();
XPath xpath = xpathFactory.newXPath();
InputSource source = new InputSource(new StringReader(xml));
status = xpath.evaluate("/resp/status/solution", source);
如果解决方案不存在,那么它将返回状态为空。现在,在使用 JXPath 时,我无法以类似的方式计算 API。这是我的示例代码
DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
DocumentBuilder bld = dbfactory.newDocumentBuilder();
文档 metaDoc = bld.parse(in);
JXPathContext metaCtx = JXPathContext.newContext(metaDoc);
节点节点 = (Node)metaCtx.selectSingleNode("/resp/status/solution");
这会引发“JXPathNotFoundException:xpath 没有值”。对于实现特定的逻辑,如果表达式不返回数据/不存在,我需要放置 if-else 块。
对此的任何指针将不胜感激。
谢谢