Xml groovy中的树排序

Xml groovy中的树排序,xml,groovy,Xml,Groovy,给出一个LinkedHashMap,我正试图在groovy中构建一个完整的xml树 1) 地图: 2) 排序树关闭: //def rslt = { [:].withDefault{ owner.call() } } def a = [] def rslt = { [:].withDefault{ owner.call() } }().with { t -> trees.each { k, v -> v.path.tokenize( '/' ).inject( t ) {

给出一个LinkedHashMap,我正试图在groovy中构建一个完整的xml树

1) 地图:

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>
[nodes: [ROOT: [nodes: [folder1: [attr: [id:1, ...], nodes: [...]]]]]

...
正在使用类似于groovy.xml.MarkupBuilder(sw.with)的sthg寻找一个闭包{

有什么想法或建议吗


BR.

通过递归遍历节点映射,可以使用
groovy.XML.StreamingMarkupBuilder
构建XML。但是,在第二步中创建的映射会丢失
树中定义的所有属性。要保留这些属性,必须先更改该部分:

// 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
    }
}
这将产生一个如下的映射:

<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>
[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
def builder=new groovy.xml.StreamingMarkupBuilder()
builder.encoding=“UTF-8”
//xml结构的闭包
def xml={
//为节点映射中的每个元素递归调用的闭包
def xmlEach
xmlEach={
键,val->

任何在Jenkins管道脚本中尝试此操作的人都应该知道