xml Groovy - 如何退出每个循环?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1766941/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Groovy - how to exit each loop?
提问by Hyman BeNimble
I'm new to Grails/Groovy and am trying to find a node in a an xml file; I've figured out how to iterate over all of them, but I want to exit the loop when the target node is found. I've read that instead of using "each", use "find", but the find examples I've seen are conditions. Right now the logic I have is going to iterate through the whole file without exiting. The code is below:
我是 Grails/Groovy 的新手,正在尝试在 xml 文件中查找节点;我已经想出了如何迭代所有这些,但我想在找到目标节点时退出循环。我读过,而不是使用“每个”,使用“查找”,但我看到的查找示例是条件。现在我的逻辑将遍历整个文件而不退出。代码如下:
records.children().each {domain ->
println "domain_name: " + domain.@domain_name
if (domain.@domain_name == targetDomain) {
println "target domain matched: " + domain.@domain_name
domain.children().each {misc_field ->
println "field_name: " + misc_field.@field_name
println "field_type: " + misc_field.@field_type
println "field_value: " + misc_field
}
}
}
回答by Rob Hruska
You cannot do it elegantly. You might see some people suggest throwing an Exception, but that's just plain ugly.
你不能优雅地做到这一点。您可能会看到有些人建议抛出一个 Exception ,但这实在是太丑了。
Here's some mailing list discussionon using eachvs. for, and a couple people say that foris preferred because of each's inability to break from the iteration.
这里有一些关于使用vs. 的邮件列表讨论,有几个人说这是首选,因为无法脱离迭代。eachforforeach
Your best bet is probably to change over to a forloop and iterate:
您最好的选择可能是切换到for循环并进行迭代:
for(def domain : records.children()) { // this may need some tweaking depending on types
// do stuff
if(condition) {
break;
}
}
Either that, or like you said, maybe use findor findAllto find the element you're looking for (the following code is paraphrased, I don't have time yet to test it out):
或者,或者像您说的那样,可以使用find或findAll来查找您要查找的元素(以下代码已解释,我还没有时间对其进行测试):
def result = records.children().find { domain -> domain.@domain_name == targetDomain }
result.children().each {
// print stuff
}
Related SO questions:
相关的问题:
回答by Michal Z m u d a
Replace eachloop with anyor findclosure.
用any或find关闭替换每个循环。
def list = [1, 2, 3, 4, 5]
list.any { element ->
if (element == 2)
return // continue
println element
if (element == 3)
true // break
}
Output
输出
1
3
回答by Alex Stoddard
Regarding breaking out of the each loop see: is it possible to 'break' out of a groovy closure
关于打破每个循环,请参阅:是否有可能“打破”常规闭包
Basically you have to throw and exception and catch it. The "break" keyword is only allowed inside loops not iterated "closures" (really code blocks).
基本上你必须抛出异常并捕获它。"break" 关键字只允许在循环内部而不是迭代"闭包"(真正的代码块)。
You can use any complex code with "find" just make sure the function you call returns a Boolean. For example:
您可以使用带有“find”的任何复杂代码,只需确保您调用的函数返回一个布尔值。例如:
Boolean test(val) {
println "Testing $val"
return val == 1
}
def found = [3,4,5,1,6,3].find { test(it) }
println "Found $found"
回答by Paull
I think this should work too, stopping at the first match.
我认为这也应该有效,在第一场比赛时停止。
def result = records.children().find { domain ->
if (domain.@domain_name == targetDomain) {
// Do stuff
...
return true
} else {
return false
}
}

