我在使用XMLSlurper时如何将谓词应用于GPath

时间:2015-07-02 15:13:50

标签: xpath groovy xmlslurper gpath

假设我有以下XML

2 Cookies
1 Chocolate
2 Brownies
10 Ice cream sandwich

我希望获得具有Productnbr = 0103的InvoiceLine的插入日期。如果我要编写xpath,我会写出如下内容:

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

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

1 个答案:

答案 0 :(得分:0)

您可以使用find进行树搜索,然后获取其子项的值:

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