Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can build your XML with <code>groovy.xml.StreamingMarkupBuilder</code> by walking recursively through the node map. The map you create in your second step loses all the attributes defined in <code>trees</code> though. To keep them you will have to change that part first:</p> <pre><code>// An empty map. Default value for nonexistent elements is an empty map. def struc = {[:].withDefault{owner.call()}}() trees.each { key, val -&gt; // iterate through the tokenized path and create missing elements along the way def substruc = struc val.path.tokenize('/').each { // if substruc.nodes does not exist it will be created as an empty map // if substruc.nodes[it] does not exist it will be created as an empty map substruc = substruc.nodes[it] } // assign the attributes to the map in .attr val.each{ attrKey, attrVal -&gt; substruc.attr[attrKey] = attrVal } } </code></pre> <p>This will result in a map like this:</p> <pre><code>[nodes: [ROOT: [nodes: [folder1: [attr: [id:1, ...], nodes: [...]]]]] </code></pre> <p>The closure that will be used in the <code>StreamingMarkupBuilder</code> will use another closure to iterate through the nodes in <code>struc</code> recursively while assigning <code>.attr</code> as the node's attributes and <code>.nodes</code> as the node's child nodes.</p> <pre><code>// we will use this builder to create our XML def builder = new groovy.xml.StreamingMarkupBuilder() builder.encoding = "UTF-8" // closure for our xml structure def xml = { // closure to be recursively called for each element in the nodes maps def xmlEach xmlEach = { key, val -&gt; out &lt;&lt; { "${key}"(val.attr) { val.nodes.each(xmlEach) } } } struc.nodes.each(xmlEachClosure) } println builder.bind(xml) </code></pre> <p>As output you get XML simliar to the following:</p> <pre><code>&lt;ROOT&gt; &lt;folder1 id='1' path='ROOT/folder1' name='folder1' isFolder='true'&gt; &lt;folder1.1 id='2' path='ROOT/folder1/folder1.1' name='folder1.1' isFolder='true'&gt; &lt;folder1.1.1 id='3' path='ROOT/folder1/folder1.1/folder1.1.1' name='folder1.1.1' isFolder='true'&gt;&lt;/folder1.1.1&gt; &lt;/folder1.1&gt; &lt;/folder1&gt; ... &lt;/ROOT&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload