开发者

Groovier way to find next XmlSlurper element in parent?

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜