如题,我想根据一个目录(包含子目录和文件)的目录树结构,用jdom生成这个目录树的xml文件,类似于下面的格式<rootdir>
<file1>....</file1>
<file2>....</file2>
<subdir1>
<file1>....</file1>
<file2>....</file2>
</subdir1>
<subdir2>
<file1>....</file1>
<file2>....</file2>
</subdir2>
....
....
....</rootdir>
请高手帮帮忙,我对xml不熟悉。。
<file1>....</file1>
<file2>....</file2>
<subdir1>
<file1>....</file1>
<file2>....</file2>
</subdir1>
<subdir2>
<file1>....</file1>
<file2>....</file2>
</subdir2>
....
....
....</rootdir>
请高手帮帮忙,我对xml不熟悉。。
解决方案 »
- axis 调用问题,如何用post XML数据调用axis的WebService
- html:select 标签的问题
- EJB3 问题
- quartz能正常启动,可是在校验quartz_xxx.xml文件时出错,高手帮忙。
- htmlparser解析网页问题
- java调用ffmpeg命令对avi文件码成flv文件的问题
- spring MVC @Autowired 失败 求解
- 想开发一个三层的网站,请高手推荐几本实用的好书!
- 各位大哥给提个建议:关于数据库设计模式问题
- MQ问题新手提问,怎么将xml文件放入MQ队列中?
- 奇怪的sql查询异常,说个啥也想不明白
- org.springframework.beans.factory.CannotLoadBeanClassException:
获取目录结构
在递归中生成xml
楼主看看一个jdom例子就ok了
import java.io.IOException;
import java.util.List;import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class Jdom {
public static void main(String[] args) {
Long lasting = System.currentTimeMillis();
SAXBuilder reader = new SAXBuilder();
Document doc = null;
try {
doc = (Document) reader.build(new File("D:\\workspace\\test\\src\\clothes.xml"));
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Element aa = doc.getRootElement();
Element bb;
List all = aa.getChildren();
for(int i=0;i<all.size();i++){
bb = (Element) all.get(i);
System.out.print(bb.getChildText("店名"));
}
}
}
我想你还没懂我的意思,我不是去读取xml文件,而是要生成我给的那种格式的xml 文件。
呵呵
只是你需要在递归生成目录结构的时候同时生成xml
这两个程序网上应该多,google,baidu一下
然后自己整理一下就出来了
还是我自己来结贴吧,我基本完成了这个功能。
http://blog.csdn.net/zzbatluzhou/archive/2010/07/19/5747783.aspx
import java.io.FileOutputStream;
import java.io.IOException;import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;public class FileXml { public static void main(String[] args) {
String path = "C:/Downloads";
File file = new File(path);
Document document = DocumentHelper.createDocument();
Element element = document.addElement("root");
element.addAttribute("path", file.getAbsolutePath());
element.addAttribute("type", "Directory");
createDocument(file, element);
writeFile(path, document);
} private static void createDocument(File file,Element parent){
File[] list = file.listFiles();
for (File f : list) {
if (f.isFile()) {
Element current = parent.addElement("child");
current.addAttribute("path", f.getAbsolutePath());
current.addAttribute("type", "File");
} else {
Element current = parent.addElement("parent");
current.addAttribute("path", f.getAbsolutePath());
current.addAttribute("type", "Directory");
createDocument(f, current);
}
}
}
private static void writeFile(String path,Document document){
FileOutputStream fos = null;
XMLWriter xw = null;
try {
fos = new FileOutputStream(path+"/tree.xml");
OutputFormat of = new OutputFormat(" ", false);
of.setEncoding("UTF-8");
xw = new XMLWriter(fos, of);
xw.write(document);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
xw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
献丑
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;public class FileXml { static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:dd");
public static void main(String[] args) {
String path = "C:/Downloads";
File file = new File(path);
Document document = DocumentHelper.createDocument();
Element element = document.addElement("root");
element.addAttribute("path", file.getAbsolutePath());
element.addAttribute("modify", sdf.format(new Date(file.lastModified())));
createDocument(file, element);
writeFile(path, document);
} private static void createDocument(File file,Element parent){
File[] list = file.listFiles();
for (File f : list) {
if (f.isFile()) {
Element current = parent.addElement("file");
current.addAttribute("path", f.getAbsolutePath());
current.addAttribute("modify", sdf.format(new Date(file.lastModified())));
} else {
Element current = parent.addElement("directory");
current.addAttribute("path", f.getAbsolutePath());
current.addAttribute("modify", sdf.format(new Date(file.lastModified())));
createDocument(f, current);
}
}
}
private static void writeFile(String path,Document document){
FileOutputStream fos = null;
XMLWriter xw = null;
try {
fos = new FileOutputStream(path+"/tree.xml");
OutputFormat of = new OutputFormat(" ", false);
of.setEncoding("UTF-8");
xw = new XMLWriter(fos, of);
xw.write(document);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
xw.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
改进。
呵呵,不好意思,我已经结贴了。
你要是早点来的话我就不必想那么久了。。
还好我最终做出来了,思路和你的一样
http://blog.csdn.net/zzbatluzhou/archive/2010/07/19/5747783.aspx