title: Groovy - how to exit each loop?
body: <p>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 &quot;each&quot;, use &quot;find&quot;, 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:</p>
  
  <pre class="lang-java prettyprint-override"><code>  records.children().each {domain -&gt;
     println &quot;domain_name: &quot; + domain.@domain_name
     if (domain.@domain_name == targetDomain) {
      
      println &quot;target domain matched: &quot; + domain.@domain_name
          
      domain.children().each {misc_field -&gt;
       
       println &quot;field_name: &quot; + misc_field.@field_name
       println &quot;field_type: &quot; + misc_field.@field_type
       println &quot;field_value: &quot; + misc_field
       
      }
     }
    }
  </code></pre>