2013-03-02 68 views
0

給一個LinkedHashMap,我試圖在groovy中構建一個完整的xml樹。Groovy中的樹排序

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> 

尋找像使用sthg像封閉groovy.xml.MarkupBuilder(sw).with {

任何想法或建議?

BR。

回答

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 simliar以下幾點:

<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> 
+0

任何人詹金斯管道腳本嘗試此應該知道[JENKINS-32766(HTTPS的://issues.jenkins-ci。組織/瀏覽/ JENKINS-32766) – Roman 2017-12-01 11:10:26