给出一个 LinkedHashMap,我正在尝试在 groovy 中构建一个完整的 xml 树。
1)地图:
def trees = [:]
trees.put(1,[id:'1',path:'ROOT/folder1',name:'folder1',isFolder:'true'])
trees.put(2,[id:'2',path:'ROOT/folder1/folder1.1',name:'folder1.1',isFolder:'true'])
trees.put(3,[id:'3',path:'ROOT/folder1/folder1.1/folder1.1.1',name:'folder1.1.1',isFolder:'true'])
trees.put(4,[id:'4',path:'ROOT/folder2',name:'folder2',isFolder:'true'])
trees.put(5,[id:'5',path:'ROOT/folder3',name:'folder3',isFolder:'true'])
trees.put(6,[id:'6',path:'ROOT/folder3/folder3.1',name:'folder3.1',isFolder:'true'])
2)排序树闭包:
//def rslt = { [:].withDefault{ owner.call() } }
def a = []
def rslt = { [:].withDefault{ owner.call() } }().with { t ->
trees.each { k, v ->
v.path.tokenize( '/' ).inject( t ) { tr, i -> tr[ i ] }
}
return t
}
3) 如何构建一个 Xml 文档,例如使用 xml slurper,
一个模型是这样的:
<ROOT>
<folder1 name="folder1" id="1" parent="ROOT" depth="1" path="ROOT/folder1">
<folder1.1 name="folder1.1" id="2" parent="folder1" depth="2" path="ROOT/folder1/folder1.1">
<folder1.1.1 name="folder1.1.1" id="3" parent="folder1.1" depth="3" path="ROOT/folder1.1/folder1.1.1"/>
</folder1.1>
</folder1>
...
</ROOT>
使用 groovy.xml.MarkupBuilder(sw).with { 之类的东西寻找闭包
有什么想法或建议吗?
BR。