最近又用到了xml的解析,这里使用一个菜单的demo来分享和回顾一下使用.
测试菜单列表
<?xml version="1.0" encoding="utf-8"?>
<root>
<AppMenu name="系统管理" open="true" iconOpen="/webmanager/resources/ztree/css/zTreeStyle/img/diy/1_open.png" iconClose="/webmanager/resources/ztree/css/zTreeStyle/img/diy/1_close.png" menuid="1000">
<Menu name="用户管理" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/9.png" url="/webmanager/user/toUserManager" menuid="10001"/>
<Menu name="部门管理" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/3.png" url="/webmanager/dept/toDeptManager" menuid="10002"/>
<Menu name="角色管理" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/8.png" url="/webmanager/role/toRoleManager" menuid="10003"/>
<Menu name="菜单权限" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/5.png" url="/webmanager/menu/menupower" menuid="10004"/>
</AppMenu>
<AppMenu name="发布管理" open="true" iconOpen="/webmanager/resources/ztree/css/zTreeStyle/img/diy/1_open.png" iconClose="/webmanager/resources/ztree/css/zTreeStyle/img/diy/1_close.png" menuid="1100">
<SubMenu name="阅读" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/9.png" menuid="11001">
<Menu name="新增阅读" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/3.png" url="" menuid="110011"/>
<Menu name="阅读列表" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/9.png" url="" menuid="110012"/>
</SubMenu>
<SubMenu name="电影" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/9.png" menuid="11002">
<Menu name="新增电影" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/3.png" url="" menuid="110021"/>
<Menu name="电影列表" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/9.png" url="" menuid="110022"/>
</SubMenu>
<SubMenu name="音乐" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/9.png" menuid="11003">
<Menu name="新增音乐" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/3.png" url="" menuid="110031"/>
<Menu name="音乐列表" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/9.png" url="" menuid="110032"/>
</SubMenu>
</AppMenu>
<AppMenu name="消息系统" open="true" iconOpen="/webmanager/resources/ztree/css/zTreeStyle/img/diy/1_open.png" iconClose="/webmanager/resources/ztree/css/zTreeStyle/img/diy/1_close.png" menuid="1200">
<Menu name="消息设置" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/9.png" url="" menuid="12001"/>
<Menu name="消息展示" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/9.png" url="" menuid="12002"/>
<Menu name="系统消息推送" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/9.png" url="/webmanager/message/sysInfo" menuid="12003"/>
<Menu name="聊天系统" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/3.png" url="" menuid="12004"/>
</AppMenu>
<AppMenu name="日志系统" open="true" iconOpen="/webmanager/resources/ztree/css/zTreeStyle/img/diy/1_open.png" iconClose="/webmanager/resources/ztree/css/zTreeStyle/img/diy/1_close.png" menuid="1300">
<Menu name="日志查询" open="true" icon="/webmanager/resources/ztree/css/zTreeStyle/img/diy/9.png" url="/webmanager/log/logSearch" menuid="13001"/>
</AppMenu>
</root>
环境准备
这里使用dom4j进行解析xml
<dependency>
<groupId>dom4j</groupId>
<artifactId>dom4j</artifactId>
<version>1.6.1</version>
</dependency>
代码开发
dom4j的使用很简单,先开发一个工具类.
public class XmlUtils {
public static Document createDocument(){
Document document=DocumentHelper.createDocument();
return document;
}
public static Element parseDocumentText(String xmlStr)
throws DocumentException {
return DocumentHelper.parseText(xmlStr).getRootElement();
}
public static Document getDocument(String filePath, String fileName)
throws Exception {
return getDocument(filePath + File.separator + fileName);
}
public static Document getDocument(String fileName) throws Exception {
FileInputStream fis = null;
try {
fis = new FileInputStream(fileName);
return getDocument(fis);
} catch (Exception e) {
throw e;
} finally {
try {
fis.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static Document getDocument(InputStream is) throws Exception {
SAXReader reader = new SAXReader();
Document document = reader.read(is);
return document;
}
public static String getTextByElementName(Element e,String Name){
String elementValue="";
Iterator iterator=e.elementIterator(Name);
if (iterator.hasNext()) {
String ElementValue = ((Element) iterator.next()).getText();
return ElementValue;
} else {
return "";
}
}
}
有了工具类我们可以生成我们的菜单树
public class MenuTreeUtil {
private static final String MENU_PATH="/config/menu.xml";
private static List<Menu> rootList;
private static Integer menutype;
public static List<Menu> ReaderMenu(int type){
menutype=type;
rootList=new ArrayList<Menu>();
SAXReader reader=new SAXReader();
try {
Document document= reader.read(MenuTreeUtil.class.getResourceAsStream(MENU_PATH));
Element root=document.getRootElement();
praseMenu(root);
return rootList;
} catch (DocumentException e) {
e.printStackTrace();
}
return null;
}
public static void praseMenu(Element root){
List<Element>elements=root.elements();
for(int i=0;i<elements.size();i++){
Menu menu=new Menu();
Element e=elements.get(i);
menu.setName(e.attributeValue("name"));
menu.setIconClose(e.attributeValue("iconClose"));
menu.setIconOpen(e.attributeValue("iconOpen"));
menu.setMenuid(e.attributeValue("menuid"));
if(Constants.MENUROLE.equals(menutype)){
menu.setOpen(true);
}else{
menu.setOpen(false);
}
if(Constants.MENUPOWER.equals(menutype)){
menu.setClick("menuPowerClick('"+e.attributeValue("menuid")+"')");
}
praseChildMenu(e,menu);
rootList.add(menu);
}
}
public static void praseChildMenu(Element root,Menu menus){
List<Element>elements=root.elements();
List<Menu> list=new ArrayList<Menu>();
for(int i=0;i<elements.size();i++){
Menu menu=new Menu();
Element e=elements.get(i);
String elementName=e.getName();
if(elementName.equals("SubMenu")){
menu.setName(e.attributeValue("name"));
menu.setIcon(e.attributeValue("icon"));
menu.setMenuid(e.attributeValue("menuid"));
if(Constants.MENUROLE.equals(menutype)){
menu.setOpen(true);
}else{
menu.setOpen(false);
}
if(Constants.MENUPOWER.equals(menutype)){
menu.setClick("menuPowerClick('"+e.attributeValue("menuid")+"')");
}
}else if(elementName.equals("Menu")){
menu.setName(e.attributeValue("name"));
menu.setIcon(e.attributeValue("icon"));
menu.setMenuid(e.attributeValue("menuid"));
if(Constants.MENUROLE.equals(menutype)){
menu.setOpen(true);
}else{
menu.setOpen(false);
}
if(Constants.MENUPOWER.equals(menutype)){
menu.setClick("menuPowerClick('')");
}else if(Constants.MENUSHOW.equals(menutype)){
menu.setClick("redirectMenu('"+e.attributeValue("url")+"')");
}
}
list.add(menu);
praseChildMenu(e, menu);
}
menus.setChildren(list);
}
}
生成完成后,我们可以结合ztree进行展示我们的菜单列表
测试
这里我们可以简单把结果打印成json格式进行输出
ReaderMenu(Constants.MENUSHOW);
Gson goson=new Gson();
System.out.println(goson.toJson(rootList));
打开App,阅读手记