title: Remove null attributes and empty children from Node
body: <p>I have a <a href="http://groovy.codehaus.org/api/groovy/util/Node.html" rel="nofollow">Node</a> which I want to remove all <code>null</code> attributes and empty children from recursively before I write it to a file.  What I have below works, but it seems like there should be a built in method for this.  Have I missed something?</p>
  
  <pre><code>Node cleanNode(Node node) {
  
      // Find null attributes
      def attributesToRemove = []
      for(e in node.attributes()) {
          if(e.value == null) {
              attributesToRemove.add(e.key)
          }
      }
  
      // Remove null attributes
      for(attribute in attributesToRemove) {
          node.attributes().remove(attribute)
      }
  
      // Clean this node's children
      for(child in node.children()) {
          if(child instanceof Node) {
              cleanNode(child)
          }
      }
  
      // If node has no attributes, no children, and no text then discard it by setting it to null
      if(!node.attributes() &amp;&amp; !node.children() &amp;&amp; !node.text()) {
          node = null
      }
  
      node
  }
  </code></pre>