【问题标题】:How to apply predicate to GPath when I am using XMLSlurper使用 XMLSlurper 时如何将谓词应用于 GPath
【发布时间】:2015-07-02 15:13:50
【问题描述】:

假设我有以下 XML

<InvoiceLines>
            <InvoiceLine>
                <InsertionDate>29.01.2015</InsertionDate>
                <Productnbr>0102</Productnbr>
            </InvoiceLine>
            <InvoiceLine>
                <InsertionDate>29.02.2015</InsertionDate>
                <Productnbr>0103</Productnbr>
            </InvoiceLine>
</InvoiceLines>

我想获得 Productnbr = 0103 的 InvoiceLine 的插入日期。如果我要写 xpath,我会写类似:

//InvoiceLine[./Productnbr='0103']/InsertionDate

但我想使用 GPath,因为我在代码中使用了 XMLSlurper。有没有办法将谓词应用于 GPath?谢谢

【问题讨论】:

    标签: xpath groovy xmlslurper gpath


    【解决方案1】:

    您可以使用带有find 的树搜索,然后获取它的孩子的值:

    def date = new XmlSlurper().parseText(xml)
                               .'**'
                               .find { it.Productnbr == '0103' }?.InsertionDate
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多