Groovy 动态修改XML

本文介绍使用Groovy语言实现功能表对象到XML树结构的转换方法。通过递归调用生成无限层级的功能树XML文件,适用于Flextree等场景。文章提供了具体的Groovy代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

我现在grails+groovy,用Groovy 生成 Flex tree 功能树需要的XML文件,里面用到了递归,可以无限级。下现有二个类:一个是对象TabFunction,这就是我的功能表影射出的对象,Test3是生成XML的测试类。(附件中有:groovy语方法文档。)
package ce.bean

class TabFunction {
String cCode;
String cName;
String cDescription;
String cParentCode;
Date dCreate=new Date();
String cState;
String cGrade
static mapping ={
version false;
}
static constraints = {

}
}


package com

import groovy.xml.StreamingMarkupBuilder
import ce.bean.TabFunction

/**
* Created by IntelliJ IDEA.
* User: mxl
* Date: 2010-3-16
* Time: 15:58:22
* To change this template use File | Settings | File Templates.
*/
class Test3 {


def str="<funlist></funlist>";
def void roolMothed(){
def obj= new TabFunction(cCode:"TOP_SysUserManager",cName:"基础信息维护",cDescription:"系统注册信息管理",cState:"0",cGrade:1,cParentCode:"00");
obj.id=2;
def obj1= new TabFunction(cCode:"SYS_UserManager",cName:"用户信息管理",cDescription:"系统注册用户管理",cState:"0",cGrade:2,cParentCode:"TOP_SysUserManager");
obj1.id=1;
def obj2= new TabFunction(cCode:"TOP_SystemManager",cName:"系统信息维护",cDescription:"系统注册信息管理",cState:"0",cGrade:1,cParentCode:"00");
obj2.id=3;
def obj3= new TabFunction(cCode:"fff",cName:"系统信息维护",cDescription:"系统注册信息管理",cState:"0",cGrade:3,cParentCode:"SYS_UserManager");
obj3.id=5;
List list=new ArrayList();
list<<obj1<<obj<<obj2<<obj3;

println "LAST:::"+ testMethod(list,list,"00",str)

}
def String testMethod(all,list,parentCode,str){
def temp = all.findAll {it.@cParentCode==parentCode};
def root = new XmlSlurper().parseText(str) ;
def outputBuilder = new StreamingMarkupBuilder()
def tempList=new ArrayList();
String result="";
if(parentCode.length()>0){
temp.each {
def tempFun=it;
//def obj=root.sysfun.find {it.@cCode == parentCode };
root.appendNode{
sysfun(cCode:tempFun.cCode,cName:tempFun.cName);
}
def ll= all.findAll {it.@cParentCode==tempFun.cCode}
if (ll.size()>0) ll.each{ tempList<<it};
}

}else{

list.each {
def tempFun=it;
// println tempFun.cParentCode+":::::"+tempFun.cCode;
def obj=root.'**'.find{it.@cCode==tempFun.cParentCode};
obj.appendNode{
sysfun(cCode:tempFun.cCode,cName:tempFun.cName);
}

def ll= all.findAll {it.@cParentCode==tempFun.cCode}
if (ll.size()>0) ll.each{ tempList<<it};


}

}

result = outputBuilder.bind{ mkp.yield root }
if(tempList.size()>0)
testMethod(all,tempList,"",result);
else{
return result;
}
}


public static void main(def a){
new Test3().roolMothed();

}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值