I have a huge xml where I need to locate the amount to pay. It is located 2 cells away from the cell with CellText = 'amount'
我有一个很大的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>
this is my code, where I am struggling how to build the expressionString:
这是我的代码,我正在努力构建expressionString:
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());
}
How do I create the correct exprString?
如何创建正确的exprString?
1 个解决方案
#1
4
An XPath expression that you could use is as follows:
可以使用的XPath表达式如下:
TableRow/Cell/CellText[text()='amount']/parent::Cell/following-sibling::Cell[2]/CellText/text()
XPathFiddle例子
This will:
这将:
- select the
CellText
element with text equal toamount
- 选择文本为amount的CellText元素
- then navigate to its parent
Cell
element - 然后导航到它的父单元格元素
- then navigate to its 2nd following
Cell
sibling - 然后导航到第二个单元格
- return the text from the
CellText
child - 返回CellText子文件中的文本
Output
输出
138
138年
#1
4
An XPath expression that you could use is as follows:
可以使用的XPath表达式如下:
TableRow/Cell/CellText[text()='amount']/parent::Cell/following-sibling::Cell[2]/CellText/text()
XPathFiddle例子
This will:
这将:
- select the
CellText
element with text equal toamount
- 选择文本为amount的CellText元素
- then navigate to its parent
Cell
element - 然后导航到它的父单元格元素
- then navigate to its 2nd following
Cell
sibling - 然后导航到第二个单元格
- return the text from the
CellText
child - 返回CellText子文件中的文本
Output
输出
138
138年