title: How to modify XML node attribute, when iterating through the file with XMLSlurper?
body: <p>In extension to <a href="https://stackoverflow.com/questions/44259910/how-to-modify-xml-node-value-when-iterating-through-the-file-with-xmlslurper">this</a> question, I am now trying to replace the XML node's attribute. </p>
  
  <p>I am already writing to the body of the XML using the below code.</p>
  
  <pre><code>def root = new XmlSlurper().parseText(
  '''&lt;root&gt;
       &lt;service&gt;
         &lt;receiver&gt;
           &lt;endpoint type="type1"&gt;123&lt;/endpoint&gt;
           &lt;endpoint&gt;456&lt;/endpoint&gt;
         &lt;/receiver&gt;
       &lt;/service&gt;
   &lt;/root&gt;''')
  
  root.service.each { service -&gt;
  service.receiver.endpoint.each { endpoint -&gt;
      endpoint.replaceBody("**"+endpoint.text())
      }
  }
  
  println groovy.xml.XmlUtil.serialize( root )
  </code></pre>
  
  <p>I want to check if the <code>type</code> attribute exists. If it does I want to change its value to say "type2". </p>
  
  <p>Is there an equivalent method to <code>replaceBody()</code> which replaces attributes? </p>
  
  <p>or do I have to implement this differently? </p>