ITPub博客

首页 > Linux操作系统 > Linux操作系统 > groovy MarkupBuilder 递归调用 recursive

groovy MarkupBuilder 递归调用 recursive

原创 Linux操作系统 作者:v_gyc 时间:2010-04-12 16:02:36 0 删除 编辑
//数据结构
def root = [name:"root-0",id:0, nodes:[[name:"node1",id:1, nodes:[[name:"node1-1",id:5]]],[name:"node2",id:2],[name:"node3",id:3]]]

//递归调用的闭包
def injectXml ={
         xml, node ->
         xml.jg(name:node.name, id:node.id){
                node.nodes?.each{
                     item->
                     injectXml(xml,item)
                }
             }
}

//程序
def writer = new StringWriter()
def xml = new MarkupBuilder(writer)
injectXml(xml, root)
def tree =  writer.toString()

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/86970/viewspace-659462/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2008-08-02

  • 博文量
    10
  • 访问量
    37766