遍历一个指定的目录(包含子目录和文件),用Jdom把该目录的树形结构写入xml文件中

最近在做一个简单的ftp客户端,基本上已经搞定了,现在的要求是给定一个远程服务器上目录,然后要我把这个目录的树形结构反应到xml文件里,注:ftp客户端是用 apache commons-net 的ftp 包,xml是用Jdom操作的。

代码如下

 


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class DirTreeXML {
   
    FTPClient fClient = ConnectServer.getftpClient();
   
    /**
     * 生成xml文件
     * @param dirPath 指定的目录的路径
     */
    public void createXML(String dirPath) {
        Element root = new Element("root");
        root.setAttribute("path", dirPath);
        Document doc = new Document(root);
         XMLOutputter XMLOut = new XMLOutputter();
         
         //设置所创建的XML文档的格式
        Format format = Format.getPrettyFormat();
        XMLOut.setFormat(format);
       
        this.createTreeElement(dirPath,root);
        try {
            XMLOut.output(doc,new FileOutputStream("dir_list.xml"));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 
    }

    /**
     * 创建整个目录的树形结构
     * @param dirPath 目录路径
     * @param fatherElement 父节点
     */
    public void createTreeElement(String dirPath,Element fatherElement) {
        try {
            FTPFile[] s = fClient.listFiles(SetCode.utf_iso(dirPath).replace("//","/"));
            for (FTPFile f : s) {
                Element currentElement = fatherElement; //当前的目录节点
                String newRemote = (dirPath + f.getName() + "/").replace("//", "/");
                if (f.isDirectory()) {
                    //Element dirElement = new Element(f.getName());
                    Element dirElement = new Element("dir");//目录节点
                    dirElement.setAttribute("name",f.getName());
                    fatherElement.addContent(dirElement);
                    currentElement = dirElement;
                    fClient.changeWorkingDirectory(SetCode.utf_iso(newRemote));
                    createTreeElement(newRemote,dirElement);
                } else {
                    Element fileElement = new Element("file").setText(f.getName());//文件节点
                    currentElement.addContent(fileElement);
                }   
            }   
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
   

    //测试函数
    public static void main(String[] args) {
        DirTreeXML test= new DirTreeXML();
        test.createXML("/新建文件夹/");
        ConnectServer.disconnect();
    }
}


 

 

生成的xml如下:

<?xml version="1.0" encoding="UTF-8"?>
<root path="/新建文件夹/">
  <dir name="新建文件夹">
    <file>新建 WinRAR 压缩文件.rar</file>
    <file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
    <file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
    <file>新建 Microsoft Word 文档.doc</file>
    <file>新建 文本文档.txt</file>
    <file>新建 WPS文字 文档.wps</file>
    <file>新建 Microsoft Excel 工作表.xls</file>
    <dir name="新建文件夹">
      <file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
      <file>新建 WPS文字 文档.wps</file>
    </dir>
  </dir>
  <dir name="新建文件夹 (2)">
    <file>新建 WinRAR 压缩文件.rar</file>
    <file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
    <file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
    <file>新建 Microsoft Word 文档.doc</file>
    <file>新建 文本文档.txt</file>
    <file>新建 WPS文字 文档.wps</file>
    <file>新建 Microsoft Excel 工作表.xls</file>
  </dir>
  <dir name="新建文件夹 (3)">
    <file>新建 WinRAR 压缩文件.rar</file>
    <file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
    <file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
    <file>新建 Microsoft Word 文档.doc</file>
    <file>新建 文本文档.txt</file>
    <file>新建 WPS文字 文档.wps</file>
    <file>新建 Microsoft Excel 工作表.xls</file>
    <dir name="新建文件夹" />
  </dir>
  <dir name="新建文件夹 (4)">
    <file>新建 WinRAR 压缩文件.rar</file>
    <file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
    <file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
    <file>新建 Microsoft Word 文档.doc</file>
    <file>新建 文本文档.txt</file>
    <file>新建 WPS文字 文档.wps</file>
    <file>新建 Microsoft Excel 工作表.xls</file>
    <dir name="新建文件夹" />
    <dir name="新建文件夹 (2)" />
  </dir>
  <dir name="新建文件夹 (5)">
    <file>新建 WinRAR 压缩文件.rar</file>
    <file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
    <file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
    <file>新建 Microsoft Word 文档.doc</file>
    <file>新建 文本文档.txt</file>
    <file>新建 WPS文字 文档.wps</file>
    <file>新建 Microsoft Excel 工作表.xls</file>
    <dir name="新建文件夹51">
      <file>新建 WinRAR 压缩文件.rar</file>
      <file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
      <file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
      <file>新建 Microsoft Word 文档.doc</file>
      <file>新建 文本文档.txt</file>
      <file>新建 WPS文字 文档.wps</file>
      <file>新建 Microsoft Excel 工作表.xls</file>
    </dir>
    <dir name="新建文件夹52">
      <file>新建 WinRAR 压缩文件.rar</file>
      <file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
      <file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
      <file>新建 Microsoft Word 文档.doc</file>
      <file>新建 文本文档.txt</file>
      <file>新建 WPS文字 文档.wps</file>
      <file>新建 Microsoft Excel 工作表.xls</file>
    </dir>
  </dir>
  <file>新建 文本文档.txt</file>
  <file>新建 Microsoft Excel 工作表.xls</file>
  <file>新建 WinRAR 压缩文件.rar</file>
  <file>新建 Microsoft PowerPoint 幻灯片.ppt</file>
  <file>新建 Microsoft PowerPoint 幻灯片 (2).ppt</file>
  <file>新建 WPS文字 文档.wps</file>
  <file>新建 Microsoft Word 文档.doc</file>
</root>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值