我能够使用XPath从XML文档成功获取值,现在用于负面场景,其中XML结构中不存在实际路径,使path = /Data/XEDA/PrimaryKey
返回null。我以为它会返回XMLParseException
。请赐教。提前致谢
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile(path);
Object result = expr.evaluate(doc);
System.out.println(result);
XML
<?xml version="1.0" encoding="UTF-8"?>
<Data BatchOrOnline="O" TotalRec="1">
<ZETA>
<PrimaryKey>1</PrimaryKey>
<Co>
<ForeignKey>3</ForeignKey>
<tblValuId>CP</tblValuId>
<tblDescTxt>Test</tblDescTxt>
</Co>
</ZETA>
答案 0 :(得分:0)
除非你的xpath实际上是错误的,否则你不会得到Xpath异常。在您的情况下,您的Xpath表达式“/ Data / XEDA / PrimaryKey”有效。所有它返回的是xml中没有这样的节点因此为null。