我有一个巨大的xml,我需要找到支付金额。它位于离细胞2个细胞的位置,CellText =&#39; <&#39;
<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>
这是我的代码,我正在努力构建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());
}
如何创建正确的exprString?
答案 0 :(得分:4)
您可以使用的XPath表达式如下:
writer.writerow([item,result.items()[0][1],result.items()[1][1]])
<强> XPathFiddle example 强>
这将:
TableRow/Cell/CellText[text()='amount']/parent::Cell/following-sibling::Cell[2]/CellText/text()
CellText
元素
amount
元素Cell
兄弟Cell
孩子<强>输出强>
138