下面是我的 连接svn的代码,在代码里面 我可以获取 svn路径下的所有文件夹以及文件
获取到的结果 如下 /trunk
/trunk/Ajax.java
/trunk/svnkit
/trunk/svnkit/svnkit.docx
/trunk/BaseServlet.java
/spring-framework-reference.pdf
/branches
/Storage.java
/员工座位表20130911.xls
/tags请问我如何才能把获取到的这些 塑造成一颗树 或者json树返回啊,谢了。什么树都可以。
package demo.wc;import java.util.Collection;
import java.util.Iterator;import org.tmatesoft.svn.core.SVNDirEntry;
import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNNodeKind;
import org.tmatesoft.svn.core.SVNURL;
import org.tmatesoft.svn.core.auth.ISVNAuthenticationManager;
import org.tmatesoft.svn.core.internal.io.svn.SVNRepositoryFactoryImpl;
import org.tmatesoft.svn.core.io.SVNRepository;
import org.tmatesoft.svn.core.io.SVNRepositoryFactory;
import org.tmatesoft.svn.core.wc.SVNWCUtil;/*
 * 此类用来显示版本库的树状结构。
 * 此类用底层API(Low Level API)直接访问版本库。
 * 此程序框架于1-5的示例(High Level API)稍有不同。
 * */
public class DisplayRepositoryTree {    public static void main(String[] args) {
//     初始化支持svn://协议的库。 必须先执行此操作。
SVNRepositoryFactoryImpl.setup();        /*
         * 相关变量赋值
         */
String url = "https://jy-PC/svn/Myjy/";//https://jy-PC/svn/Myjy/
        String name = "admin";
        String password = "admin";
        //定义svn版本库的URL。
        SVNURL repositoryURL = null;
        //定义版本库。
        SVNRepository repository = null;
        /*
         * 实例化版本库类
         * */
        try {
         //获取SVN的URL。
         repositoryURL=SVNURL.parseURIEncoded(url);
         //根据URL实例化SVN版本库。
            repository = SVNRepositoryFactory.create(repositoryURL);
        } catch (SVNException svne) {
            /*
             * 打印版本库实例创建失败的异常。
             */
            System.err
                    .println("创建版本库实例时失败,版本库的URL是 '"
                            + url + "': " + svne.getMessage());
            System.exit(1);
        }        /*
         * 对版本库设置认证信息。
         */
        ISVNAuthenticationManager authManager = SVNWCUtil.createDefaultAuthenticationManager(name, password);
        repository.setAuthenticationManager(authManager);        /*
         * 上面的代码基本上是固定的操作。
         * 下面的部门根据任务不同,执行不同的操作。
         * */
        try {            //打印版本库的根
//            System.out.println("Repository Root: " + repository.getRepositoryRoot(true));
            //打印出版本库的UUID
//            System.out.println("Repository UUID: " + repository.getRepositoryUUID(true));
            System.out.println("");
            //打印版本库的目录树结构
            listEntries(repository, "");
        } catch (SVNException svne) {
            System.err.println("打印版本树时发生错误: "
                    + svne.getMessage());
            System.exit(1);
        }
        /*
         * 获得版本库的最新版本树
         */
        long latestRevision = -1;
        try {
            latestRevision = repository.getLatestRevision();
        } catch (SVNException svne) {
            System.err
                    .println("获取最新版本号时出错: "
                            + svne.getMessage());
            System.exit(1);
        }
        System.exit(0);
    }
    /*
     * 此函数递归的获取版本库中某一目录下的所有条目。
     */
    public static void listEntries(SVNRepository repository, String path)
            throws SVNException {
        //获取版本库的path目录下的所有条目。参数-1表示是最新版本。
        Collection entries = repository.getDir(path, -1, null,(Collection) null);
        Iterator iterator = entries.iterator();
        while (iterator.hasNext()) {
            SVNDirEntry entry = (SVNDirEntry) iterator.next();
            System.out.println("/" + (path.equals("") ? "" : path + "/") + entry.getName());
            if (entry.getKind() == SVNNodeKind.DIR) {
                listEntries(repository, (path.equals("")) ? entry.getName(): path + "/" + entry.getName());
            }
        }
    }
}java 树 svn

解决方案 »

  1.   

    设计好树数据结构:import java.util.List;
    public class FileTree
    {    private String name;//路径名    private FileTree fatherFile;//父目录    private List<FileTree> childrenFileList;//子文件列表    public FileTree(String name)
        {
            this.name = name;
        }    //getter/setter ...
    }后面为了方便判断,可以再根据需要设计几个辅助方法。
    至于改造json字符串,有了实体类,递归的思想,应该不难搞吧。
      

  2.   

    把SVN的文件下载下来,通过dos命令可以生成目录结构树,把工作空间前的路径替换掉,剩下的就是svn上目录路径了