使用xml或者json方式生成dhtmlxtree

阅读更多
1. dao
private static ParameterizedRowMapper menuInfoMapper = new ParameterizedRowMapper() {
public MenuInfo mapRow(ResultSet rs, int rowNum) throws SQLException {
MenuInfo menuInfo = new MenuInfo();
menuInfo.setHasit(rs.getString("hasit"));
menuInfo.setPmenuid(rs.getLong("pmenuid"));
menuInfo.setLevel(rs.getLong("level"));
menuInfo.setId(rs.getLong("id"));
menuInfo.setMsrtno(rs.getLong("msrtno"));
menuInfo.setMenunm(rs.getString("menunm"));

menuInfo.setLocation(rs.getString("location"));
menuInfo.setActiontyp(rs.getLong("actiontyp"));
menuInfo.setHlpid(rs.getLong("hlpid"));
menuInfo.setLfabl(rs.getString("lfabl"));
menuInfo.setFbdst(rs.getString("fbdst"));
return menuInfo;
}
};
public Collection testPrivilege() {
final String sql = "select nvl((select 'a' from wpv0105_rlprv b where b.lbl='0' and b.rlid=? and b.prvid=a.id),'b') as hasit, "
+" a.pmenuid,level, a.id, a.msrtno, a.menunm, a.location, a.actiontyp, a.hlpid, a.lfabl,a.fbdst "
+" from wpv0101_menu a start with pmenuid=-1 connect by prior id = pmenuid and a.id<>100031401 "
+" group by pmenuid,level, id, msrtno, menunm, location, actiontyp, hlpid, lfabl,fbdst "
+" order by id||'0', msrtno";
return jdbcTemplate.query(sql, menuInfoMapper, new Object[]{new Long(0)});
}

2. 数据封装
(1) xml
@Test public void testMenu() throws Exception {
java.util.Collection menuInfoList = mobileDao.testPrivilege();
java.io.OutputStream out = null;
java.io.OutputStreamWriter dataout = null;
out = new java.io.FileOutputStream("aaa.xml");
dataout = new java.io.OutputStreamWriter(out, "UTF-8");
dataout.write("\n");
int level=0;
for(MenuInfo vo : menuInfoList)
{
if(level == Integer.parseInt(vo.getLevel()+"")) {
dataout.write("\r\n");
final String chk = "a".equals(vo.getHasit())?"checked=\"1\"":"";
dataout.write("");

}
else if(Integer.parseInt(vo.getLevel()+"") > level) {
final String chk = "a".equals(vo.getHasit())?"checked=\"1\"":"";
dataout.write("  ");
}
else {
for(int i =0; i < level + 1 - Integer.parseInt(vo.getLevel()+""); i++)
dataout.append("
\r\n");
final String chk = "a".equals(vo.getHasit())?"checked=\"1\"":"";
dataout.write("");
}
level = Integer.parseInt(vo.getLevel()+"");
}
for(int j =0; j dataout.write("
\r\n");
dataout.write("
");
dataout.flush();
dataout.close();
out.close();
}

(2) json
@Test public void testMenuJSON() throws Exception {
java.util.Collection menuInfoList = mobileDao.testPrivilege();
java.io.OutputStream out = null;
java.io.OutputStreamWriter dataout = null;
out = new java.io.FileOutputStream("bbb.JSON");
dataout = new java.io.OutputStreamWriter(out, "UTF-8");
dataout.write("{id:'0' ");
int level=0;
for(MenuInfo vo : menuInfoList)
{
if(level == Integer.parseInt(vo.getLevel()+"")) {
dataout.write("}\r\n");
final String chk = "a".equals(vo.getHasit())?"checked:'1', ":"";
dataout.write(",{" + "id:'" + vo.getId()+ "', " + chk + "text:'"+ vo.getMenunm() + "', im0:'leaf.gif', im1:'folderOpen.gif', im2:'folderClosed.gif'");

}
else if(Integer.parseInt(vo.getLevel()+"") > level) {
final String chk = "a".equals(vo.getHasit())?"checked:'1', ":"";
dataout.write(", item:[{" + "id:'" + vo.getId()+ "', " + chk + "text:'"+ vo.getMenunm() + "', im0:'leaf.gif', im1:'folderOpen.gif', im2:'folderClosed.gif'");
}
else {
for(int i =0; i < level - Integer.parseInt(vo.getLevel()+""); i++)
dataout.append("}\r\n]\r\n");
final String chk = "a".equals(vo.getHasit())?"checked:'1', ":"";
dataout.write("}\r\n,{" + "id:'" + vo.getId()+ "', " + chk + "text:'"+ vo.getMenunm() + "', im0:'leaf.gif', im1:'folderOpen.gif', im2:'folderClosed.gif'");
}
level = Integer.parseInt(vo.getLevel()+"");
}
for(int j =0; j dataout.write("}]\r\n");
dataout.write("}");
dataout.flush();
dataout.close();
out.close();
}

3. 我对比了一下,若不使用专业版的dhtmltree的情况下,都加载大数据,使用xml和json的速度一样慢,几乎没有区别。主要还是渲染生成树的计算太复杂,若使用专业版(太贵了,都要800多美元),并把它的 tree.enableSmartXMLParsing(true) 因为它在你查看该节点时才加载它的子节点,所以树能很快显示,它还有一点做的特别好就是在树的有些节点没有被加载的情况下,通过tree.getAllChecked()也能够获取到这些节点。
  • b.rar (114.3 KB)
  • 下载次数: 487

你可能感兴趣的:(XML,json,Java,SQL,J#)