簡體   English   中英

groovy中的樹排序

[英]tree sorting in groovy

給定一個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 slurper構建Xml文檔,

一個模型是這樣的:

<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).sthg之類的{

有什么想法或建議嗎?

BR。

您可以通過遞歸地遍歷節點圖來使用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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM