import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class QxglController extends BaseController {
/**
* 导出menu权限
*/
public void xmlExp(){
String xtbs = getPara("xtbs");
try {
String sql = "";
YwxtQx qx = YwxtQx.dao.findFirst(sql,xtbs);
// 1、创建document对象
Document document = DocumentHelper.createDocument();
// 2、创建根节点rss
//一级目录
Element menu1 = document.addElement("menu");
// 3、向menu节点添加version属性
menu1.addAttribute("code", qx.getStr("QXDM"));
menu1.addAttribute("name", qx.getStr("QXMC"));
menu1.addAttribute("url", qx.getStr("QXDZ") == null ? "":qx.getStr("QXDZ"));
//根据主菜单 pid = 0 查询子菜单
getChild(menu1,qx.getBigDecimal("ID"));
// 5、设置生成xml的格式
OutputFormat format = OutputFormat.createPrettyPrint();
// 设置编码格式
format.setEncoding("UTF-8");
// 6、生成xml文件
File file = new File("menu.xml");
XMLWriter writer = new XMLWriter(new FileOutputStream(file), format);
// 设置是否转义,默认使用转义字符
writer.setEscapeText(false);
writer.write(document);
writer.close();
//返回到页面并下载
renderFile(file);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 递归menu权限
* @param e
* @param pid
*/
public void getChild(Element e,BigDecimal pid){
String sql1 = "";
List<YwxtQx> qxChild = YwxtQx.dao.find(sql1,pid);
if(qxChild.size() > 0){
for(YwxtQx qxch:qxChild){
//二级目录
// 4、生成子节点及子节点内容
Element menu2 = e.addElement("menu");
menu2.addAttribute("code", qxch.getStr("QXDM"));
menu2.addAttribute("name", qxch.getStr("QXMC"));
menu2.addAttribute("url", qxch.getStr("QXDZ") == null ? "":qxch.getStr("QXDZ"));
//递归所有的子节点
getChild(menu2,qxch.getBigDecimal("ID"));
}
}
}
}
参考于修改:https://blog.csdn.net/qq_39237801/article/details/78378486