如题,我想根据一个目录(包含子目录和文件)的目录树结构,用jdom生成这个目录树的xml文件,类似于下面的格式<rootdir>
    <file1>....</file1>
    <file2>....</file2>
    <subdir1>
        <file1>....</file1>
        <file2>....</file2>
    </subdir1>
    <subdir2>
        <file1>....</file1>
        <file2>....</file2>
    </subdir2>
      ....
      ....
      ....</rootdir>
请高手帮帮忙,我对xml不熟悉。。

解决方案 »

  1.   

    递归
    获取目录结构
    在递归中生成xml
    楼主看看一个jdom例子就ok了
      

  2.   

    import java.io.File;
    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("店名"));
            }
        }
    }
      

  3.   

    有什么问题大家探讨一下 加QQ群 深圳J2EE①群 32763598 验证 SZJP
      

  4.   

    to : ymdcr
    我想你还没懂我的意思,我不是去读取xml文件,而是要生成我给的那种格式的xml 文件。
      

  5.   

    我知道你的意思
    呵呵
    只是你需要在递归生成目录结构的时候同时生成xml
    这两个程序网上应该多,google,baidu一下
    然后自己整理一下就出来了
      

  6.   

    这是我第一次发贴诶。结果都没多少人来凑热闹看来以后没深度的问题还真不应该拿来发贴!
    还是我自己来结贴吧,我基本完成了这个功能。
    http://blog.csdn.net/zzbatluzhou/archive/2010/07/19/5747783.aspx
      

  7.   

    没做过转换XML的。类似tree的倒是做过。
      

  8.   

    package com.xuz.csdn.june19;import java.io.File;
    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();
    }
    }
    }
    }
    献丑
      

  9.   

    package com.xuz.csdn.june19;import java.io.File;
    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();
    }
    }
    }
    }
    改进。
      

  10.   


    呵呵,不好意思,我已经结贴了。
    你要是早点来的话我就不必想那么久了。。
    还好我最终做出来了,思路和你的一样
    http://blog.csdn.net/zzbatluzhou/archive/2010/07/19/5747783.aspx