0

给出一个 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。

4

1 回答 1

0

groovy.xml.StreamingMarkupBuilder您可以通过递归遍历节点映射来构建您的 XML 。您在第二步中创建的地图会丢失其中定义的所有属性trees。要保留它们,您必须先更改该部分:

// An empty map. Default value for nonexistent elements is an empty map.
def struc = {[:].withDefault{owner.call()}}()

trees.each { key, val ->
    // 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 ->
        substruc.attr[attrKey] = attrVal
    }
}

这将产生这样的地图:

[nodes: [ROOT: [nodes: [folder1: [attr: [id:1, ...], nodes: [...]]]]]

将在 中使用的StreamingMarkupBuilder闭包将使用另一个闭包以递归方式遍历节点,struc同时分配.attr为节点的属性和.nodes节点的子节点。

// 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 ->
            out << {
                "${key}"(val.attr) {
                    val.nodes.each(xmlEach)
                }
            }
    }
    struc.nodes.each(xmlEachClosure)
}

println builder.bind(xml)

作为输出,您将获得类似于以下内容的 XML:

<ROOT>
    <folder1 id='1' path='ROOT/folder1' name='folder1' isFolder='true'>
        <folder1.1 id='2' path='ROOT/folder1/folder1.1' name='folder1.1' isFolder='true'>
            <folder1.1.1 id='3' path='ROOT/folder1/folder1.1/folder1.1.1' name='folder1.1.1' isFolder='true'></folder1.1.1>
        </folder1.1>
    </folder1>
...
</ROOT>
于 2013-03-04T14:04:50.070 回答