xmlgroovyxmlslurpergpath

How to get the next sibling with GPathResult


How can I get the next sibling of an GPathResult? For example I have the following code:

def priorityIssue = xmlReport.'**'.find { Issue ->
   Issue.Priority.text() == priority
}

How do I get priorityIssue's next sibling?

Thanks!


Solution

  • More or less this is the way to go:

    import groovy.util.XmlSlurper
    
    def xml = new XmlSlurper().parseText('''
    <issues>
        <issue>
            <id>1</id>
            <priority>1</priority>
        </issue>
        <issue>
            <id>2</id>
            <priority>2</priority>
        </issue>
    </issues>
    ''')
    def p = '1' 
    def priorityIssue = xml.'**'.find { issue ->
        issue.priority.text() == p
    }
    def kids = priorityIssue.parent().children().list()
    def idx = kids.indexOf(priorityIssue)
    def sibling = kids[++idx]
    assert sibling.id.text() == '2'