67,513
社区成员
发帖
与我相关
我的任务
分享
Document document = DocumentHelper.createDocument();
Element root = document.addElement("Root");
int j=0;
// 递归方法
public String tree(String path, int level) {
// 创建xml对象"D:\\tomcat6\\webapps\\asset\\pandian"
File f = new File(path);
String preStr = "";
for (int i = 0; i < level; i++) {
preStr += "-";
}
File[] childs = f.listFiles();
System.out.println(childs.length+"");
for (int i = 0; i < childs.length; i++) {
System.out.println(preStr + childs[i].getName());
if (childs[i].isDirectory()) {
if("pandian".equals(childs[i].getName())){
continue;
}
Element firstElement = root.addElement("firstMenu");
firstElement.addAttribute("id", i+"");
firstElement.addAttribute("label",childs[i].getName());
firstElement.addAttribute("icon", "file.jpg");
j=i;
tree(childs[j].toString(), level + 1);
}
else{
String xPath = "/Root/firstMenu[@id='" + j + "']";
Element firstElement = (Element) document.selectNodes(xPath)
.get(0);
Element secondElement = firstElement.addElement("secondMenu");
secondElement.addAttribute("id", i+"");
secondElement.addAttribute("label",childs[i].getName() );
secondElement.addAttribute("icon", "pcj.jpg");
}
}
// writXml(document);
return document.asXML();
}
public void clearxml(){
// if(document!=null)
// if (root.isTextOnly()) {
// root.remove(root);
// }
// document.clearContent();
}
在调用上面方法之前先清空 但是报错··