开发者

Groovier way to find next XmlSlurper element in parent?

开发者 https://www.devze.com 2023-01-03 06:18 出处:网络
I am using: def idx=parent.item.children().indexOf(myElement) if (idx+1<parent.children().size()) { def message=parent.children()[idx+1]

I am using:

def idx=parent.item.children().indexOf(myElement)
if (idx+1<parent.children().size()) {
  def message=parent.children()[idx+1]
  println message.text()
}
开发者_运维百科

To find the element message which is next after myelement in the parent.

However, it seems there must be a Groovier way, no?

Thank you Misha


Assuming you're trying to find all after the first one you would do this:

def messages = parent.item.children().findAll { child -> child.name() == myElement }
messages[1..-1].each { println it } 

If you wanted to just print out all messages that matched myElement

parent.item.children().findAll { child -> child.name() == myElement }.each { println it } 
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号