Search code examples
xmlgroovydoublenodesxmlslurper

groovy xmlSlurper check more fields within same level


I have this xml.

<?xml version="1.0" encoding="utf-8"?>
<entry>
    <id>E0000</id>
    <link href="href">
        <inline>
            <entry>
                <link href="href">
                    <inline>
                        <feed>
                            <entry>
                                <id>E0001</id>
                                <content type="application/xml">
                                    <props>
                                        <status/>
                                    </props>
                                </content>
                            </entry>
                            <entry>
                                <id>E0002</id>
                                <content type="application/xml">
                                    <props>
                                        <status/>
                                    </props>
                                </content>
                            </entry>
                            <entry>
                                <id>E0003</id>
                                <content type="application/xml">
                                    <props>
                                        <status>S00</status>
                                    </props>
                                </content>
                            </entry>
                        </feed>
                    </inline>
                </link>
            </entry>
        </inline>
    </link>
</entry>

I'm using xmlSlurper to check whether among the deepest "entry" tags, there's one having both "id" = "E0001" AND "status" = "S00" or "id" = "E0002" AND "status" = "S00". Something like this: (id=E0001 AND status=S00) OR (id=E0002 AND status=S00).

I'm using this code (I'm testing it with Groovy Web Console).

    def text = '<?xml version=\"1.0\" encoding=\"utf-8\"?><entry><id>E0000</id><link href=\"href\"><inline><entry><link href=\"href\"><inline><feed><entry><id>E0001</id><content type=\"application/xml\"><props><status>S00</status></props></content></entry><entry><id>E0002</id><content type=\"application/xml\"><props><status/></props></content></entry><entry><id>E0003</id><content type=\"application/xml\"><props><status/></props></content></entry></feed></inline></link></entry></inline></link></entry>'
def response = new XmlSlurper().parseText(text)
def result = (response.link.inline.entry.link.inline.feed.find {(it.entry.content.props.status.text() == 'S00' & it.entry.id.text().contains('E0001')) | ((it.entry.content.props.status.text() == 'S00' & it.entry.id.text().contains('E0002')))}).size() > 0 ? 'true' : 'false'
println(result)

But I'm getting true as result even if the status=S00 is under the "entry" tag with id=E0003 which is unwanted. How can I tweak my above code?


Solution

  • Thanks for the advice Cfrick. I've adjusted it so and it works.

    def result = (response.link.inline.entry.link.inline.feed.entry.find {(it.content.props.status.text() == 'S00' & it.id.text().contains('E0001')) | ((it.content.props.status.text() == 'S00' & it.id.text().contains('E0002')))}).size() > 0 ? 'true' : 'false'