向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的树形目录。