title: create xml markupBuilder based on external variables
body: <p>I have seen in plenty of websites the common use of MarkupBuilder xml creator. For example: </p>
  
  <pre><code>def xmlWriter = new StringWriter()
  def xmlMarkup = new MarkupBuilder(xmlWriter)
  xmlMarkup.movie(id: "2", "the godfather")
  println(xmlWriter.toString())
  </code></pre>
  
  <p>Will give something like this: </p>
  
  <pre><code>&lt;movie id='2'&gt;the godfather&lt;/movie&gt;
  </code></pre>
  
  <p>My question is: ¿Is there a good way to use MarkupBuilder to compose an xml using variables taken from a method?</p>
  
  <p>I have manage to add the root access with this code:</p>
  
  <pre><code>createXml(root){
      def xmlWriter = new StringWriter()
      def xmlMarkupBuilder = new MarkupBuilder(xmlStringWriter)
      xmlMarkupBuilder.createNode(root)
      xmlMarkupBuilder.nodeCompleted(null,root)
  }
  </code></pre>
  
  <p>But I'm pretty sure there must be another clean way to do it. How can I add a new node if I only know the name of the parent node?</p>
  
  <p>I ended up doing this with java DocumentBuilder, </p>
  
  <pre><code>    XmlData(xmlPath, rootNodeName){
         this.xmlPath = xmlPath
         xmlDoc =    DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
         def rootNode=xmlDoc.createElement(rootNodeName)
         xmlDoc.appendChild(rootNode)
     }
  
      def addNode(parentNodeName,nodeName,nodeValue,attributes){
         def newNode = xmlDoc.createElement(nodeName)
         if(nodeValue!=null){
            def newTextNode = xmlDoc.createTextNode(nodeValue)
            newNode.appendChild(newTextNode)
         }
         if(attributes!=null){
            attributes.each{key,value -&gt;
              newNode.setAttribute($key,$value)
            }
         }
         def nodeList = xmlDoc.getElementsByTagName(parentNodeName)
         nodeList.item(nodeList.getLength()-1).appendChild(newNode)
     }
  </code></pre>
  
  <p>but if there is a cleaner way to do this with MarkupBuilder or MarkupBuilderHelper, I would preffer to use that one. The code I would like to get is: </p>
  
  <p>Input: </p>
  
  <pre><code>        def xmlWriter = new XmlWriter("rootNode")
          xmlWriter.addNode("rootNode","",null)
          xmlWriter.addNode("rootNode","child2",null)
          xmlWriter.addNode("child1","child11","text1")
          xmlWriter.addNode("child1","child12","text2")
          xmlWriter.addNode("child2","child21","text3")
          xmlWriter.addNode("child2","child22","text4")
  </code></pre>
  
  <p>Methods: </p>
  
  <pre><code>class XmlWriter{
      createXml(root){
      def xmlWriter = new StringWriter()
      def xmlMarkupBuilder = new MarkupBuilder(xmlStringWriter)
      xmlMarkupBuilder.createNode(root)
      xmlMarkupBuilder.nodeCompleted(null,root)
      }
  
      def addNode(parentNodeName,nodeName,nodeValue,attributes){
         ???
      }
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>&lt;rootNode&gt;
    &lt;child1&gt;
       &lt;child11&gt;test1&lt;/child11&gt;
       &lt;/child12&gt;test2&lt;/child12&gt;
    &lt;/child1&gt;
    &lt;child2&gt;
       &lt;child21&gt;test3&lt;/child21&gt;
       &lt;/child22&gt;test4&lt;/child22&gt;
    &lt;/child2&gt;
  &lt;/rootNode&gt;
  </code></pre>
  
  <p>Note: I don't take into account that could exist several items in nodeList items because in my xml this is not possible at the present moment.</p>