【发布时间】:2016-02-29 22:25:37
【问题描述】:
我有一个巨大的 xml,我需要在其中找到要支付的金额和对应的 billID。
我刚刚想出了如何选择正确的数量(与 CellText = 'amount' 的单元格相距 2 个单元格)。我可以打印这个,它可以工作。
现在我还需要打印相应的 billID。我该怎么做?
这是我正在使用的 XML 类型的示例:
<Section>
<Item>
<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>
</Item>
<Item>
<BillID>123456</BillID>
</Item>
</Section>
<Section>
...
</Section>
这是我的代码,我需要在其中添加 billID 也应该打印:
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());
}
(我知道 XML 没有意义,它只是为了说明格式。还有几个“部分”,所以我需要确保选择正确的一个!)。提前致谢!
【问题讨论】:
-
当然还有几个节点对象,所以我真的很想选择正确的一个!
-
您应该更喜欢通过编辑而不是编写 cmets 向您的问题添加其他信息。
-
完成!当您花时间发表评论时,您现在可以帮我解答吗;)
-
没有。但我可以链接到this SO question。它看起来和你的几乎一模一样。
-
是的,但这是接下来的问题......