特定のノードのIDとパスのマップが与えられたら、xmlツリーを構築します。
def trees = [:]
trees.put(1,"TEST/folder1")
trees.put(2,"TEST/folder2")
trees.put(3,"TEST/folder1/folder1.1")
trees.put(4,"TEST/folder2/folder2.1/folder2.1.2")
trees.put(5,"TEST/folder1/folder1.2")
trees.put(6,"TEST/folder1/folder1.2/folder1.2.1/")
trees.put(7,"TEST/folder1/folder1.2/folder1.2.2/")
trees.put(8,"TEST/folder1/folder1.2/folder1.2.2/1.2.2.1")
sw = new StringWriter()
def rslt = { [:].withDefault{ owner.call() } }().with { t ->
trees.each { k, v ->
v.tokenize( '/' ).inject( t ) { tr, i -> tr[ i ] }
}
return t
}
new groovy.xml.MarkupBuilder(sw).with {
visitor = { k, v -> "$k" { v instanceof Map ? v.collect(visitor) : mkp.yield(v) } }
ROOT { rslt.collect visitor }
}
println sw.toString()
そして最後にこの出力を取得します:
<TEST>
<folder1>
<folder1.1 />
<folder1.2>
<folder1.2.1 />
<folder1.2.2>
<1.2.2.1 />
</folder1.2.2>
</folder1.2>
</folder1>
<folder2>
<folder2.1>
<folder2.1.2 />
</folder2.1>
</folder2>
</TEST>
</ROOT>
初期のツリーマップ値から継承された属性ID、名前、親の深さ、URLを追加するグルービーな方法はありますか?
整形式のxmlドキュメントの例:
<folder2 name="folder2" id="2" parent="TEST" depth="1" url="TEST/folder2">
<folder2.1.1 name="folder2.1.1" id="2" parent="TEST" depth="2" url="TEST/folder2">
<folder2.1.1.1 name="folder2.1.1.1" id="3" parent="TEST" depth="3" url="TEST/folder2"/>
</folder2.1.1>
</folder2>
アイデアや提案はありますか?
乾杯