How to apply predicate to GPath when I am using XMLSlurper

孤者浪人 提交于 2019-12-11 20:37:17

问题


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


回答1:


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


来源:https://stackoverflow.com/questions/31188439/how-to-apply-predicate-to-gpath-when-i-am-using-xmlslurper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!