【问题标题】:xPath retrieve multiple valuesxPath 检索多个值
【发布时间】: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。它看起来和你的几乎一模一样。
  • 是的,但这是接下来的问题......

标签: xml xpath nodelist


【解决方案1】:

如果您想同时获得Strings,您可以使用以下XPath 表达式应用节点统一运算符| 相对于假设的root 节点:

/root/Section/Item/TableRow/Cell/CellText[text()='amount']/parent::Cell/following-sibling::Cell[2]/CellText/text() | /root/Section/Item/BillID/text()

可以查看here at XPathFiddle。 这导致

138
123456

【讨论】:

    【解决方案2】:

    这个 sn-p 可以做到。从您选择金额的节点中,找到祖先Item 节点,转到下一个兄弟节点并选择BillID 文本。选择金额后,这将使用以下 XPath:

    ancestor::Item[1]/following-sibling::Item[1]/BillID/text()
    

    import java.io.StringReader;
    import javax.xml.xpath.*;
    import org.w3c.dom.*;
    import org.xml.sax.InputSource;
    
    public class GetTheBillId {
        private static final String xml1=
    "<root>"+
    "<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>"+
    "</root>";
    
        private static final String xpathExpr1=
    "//TableRow/Cell[CellText='amount']/following-sibling::Cell[2]/CellText/text()";
    
        private static final String xpathExpr2=
    "ancestor::Item[1]/following-sibling::Item[1]/BillID/text()";   
    
        public static void main(String[] args) {
            try {
                XPath xpath = XPathFactory.newInstance().newXPath();
                XPathExpression expr1 = xpath.compile(xpathExpr1);
                XPathExpression expr2 = xpath.compile(xpathExpr2);
                NodeList nodeList = (NodeList) expr1.evaluate(new InputSource(new StringReader(xml1)),XPathConstants.NODESET);
                StringBuilder sb = new StringBuilder();
                for( int i = 0; i != nodeList.getLength(); ++i ) {
                    Node n = (Node) nodeList.item(i);
                    sb.append( "Amount=" ).append(n.getNodeValue());
    
                    Node billId = (Node) expr2.evaluate(n,XPathConstants.NODE); // search for Bill ID
                    sb.append("; BillId=").append(billId.getNodeValue());
                    sb.append(System.lineSeparator());
                }
                System.out.println(sb.toString());
            } catch (XPathExpressionException e) {
                e.printStackTrace();
            }
        }
    }
    

    输出:

    Amount=138; BillId=123456
    

    【讨论】:

      猜你喜欢
      • 2017-08-13
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      • 2013-01-08
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多