我能够使用 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>