我有一个巨大的 XML,我需要在其中找到支付金额。它距离单元格 2 个单元格CellText = 'amount'
<TableRow>
<Cell>
<RowNo>4</RowNo>
<CellColNo>1</CellColNo>
<CellText>amount</CellText>
<CellAttribute/>
</Cell>
<Cell>
<RowNo>4</RowNo>
<CellColNo>2</CellColNo>
<CellText/>
<CellAttribute/>
</Cell>
<Cell>
<RowNo>4</RowNo>
<CellColNo>3</CellColNo>
<CellText>138</CellText>
<CellAttribute/>
</Cell>
</TableRow>
代码,我正在努力如何构建表达式字符串:
XPath xPath = XPathFactory.newInstance().newXPath();
String exprString =
"//TableRow/Cell[CellText='amount:']/following-sibling::cell[2]CellText/text()";
XPathExpression expr = xPath.compile(exprString);
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int j = 0; j < nodes.getLength(); j++) {
System.out.println(nodes.item(j).getNodeValue());
}
我如何创建正确的exprString
?
您可以使用的 XPath 表达式如下:
TableRow/Cell/CellText[text()='amount']/parent::Cell/following-sibling::Cell[2]/CellText/text()
XPathFiddle 示例 http://xpathfiddle.net/E67N5S
这会:
- 选择
CellText
文本等于的元素amount
- 然后导航到其父级
Cell
element
- 然后导航到第二个以下
Cell
sibling
- 返回文本
CellText
child
Output
138
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)