部门bean主要代码
private String deptname;//部门名称 private String deptcode;//部门编码 private String fdeptcode;//上级部门编码 private String isleaf;//是否为叶子部门 private List<Dep> children;//子部门集合 private String id;//部门id //设置如何获取子部门 public void setChildren(String fdeptcode) { if(isleaf.equals("1")){ }else{ List<Dep> listDep=new ArrayList<Dep>(); DbConn db=new DbConn(); ResultSet rst=null; String sql1="select deptcode from comdept where fdeptcode=‘"+fdeptcode+"‘"; try { rst=db.executeQuery(sql1); while(rst.next()){ String depcode=rst.getString("deptcode"); //调用构造方法如果dep还有子部门也会放到dep下 Dep dep=new Dep(depcode); listDep.add(dep); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } this.children = listDep; } } //构造方法 public Dep(String deptcode){ this.deptcode=deptcode; setAll(deptcode); setChildren(deptcode); }
向xml写树形目录代码
public class WriteToXml { static List<Dep> listdep=new Dep("0000"); //0000为部门根 Element root=DocumentHelper.createElement("Dep"); Document doc=DocumentHelper.createDocument(); public void Write(List<Dep> listdep,Element element){ for(Dep dep:listdep){ //添加节点名字为部门名字 Element menuElement = element.addElement(dep.getDeptname()); //向节点添加属性deptcode,value为部门id Attribute att=DocumentHelper.createAttribute(menuElement, "deptcode",dep.getDeptcode()); menuElement.add(att); if(dep.getChildren()!=null&&dep.getChildren().size()>0){ Write(dep.getChildren(),menuElement); } } } public void WriteAll(String path){ //全部节点写到root节点 Write(listdep,root); try{ XMLWriter xmlwriter=new XMLWriter(new FileWriter(new File(path))); doc.add(root); xmlwriter.write(doc); xmlwriter.close(); }catch(IOException e){ e.printStackTrace(); } } public static void main(String[] args) { WriteToXml t=new WriteToXml(); t.WriteAll("E://test.xml"); } }
好不容易终于大概知道xml是怎么写的了...下一步再把xml形成html的树形目录。
时间: 2024-10-07 16:11:27