如何在使用XMLSlurper时将谓词应用于GPath [英] How to apply predicate to GPath when I am using XMLSlurper

查看:150
本文介绍了如何在使用XMLSlurper时将谓词应用于GPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以说我拥有以下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?谢谢

解决方案

您可以使用 find 进行树搜索,然后获得它的孩子的价值:

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


lets say I have the following XML

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

and I would like to get the insertion date of InvoiceLine that has Productnbr = 0103. If I would write xpath I would write somthing like:

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

but I would like to use GPath since I am using XMLSlurper in my code. Is there a way how to apply predicate to GPath? Thank you

解决方案

You can use a tree search with find and then get the value of it's child:

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

这篇关于如何在使用XMLSlurper时将谓词应用于GPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆