点点滴滴

不劳而得之财,必然消耗;勤劳积蓄的,必见加增。
随笔 - 2, 文章 - 0, 评论 - 2, 引用 - 0
数据加载中……

转换Sogou浏览器导出的收藏夹

        Sogou浏览器的导出的收藏夹是xml格式的,其他浏览器不能导入对应的文件,我编写了一个Java类,解析Sogou浏览器导出的文件,并在本地创建IE可以认识的格式。

        这些代码使用到了Jdom解析XML文件,以及创建IE快捷方式的方法。

        IE快捷方式文件的格式如下:
1[InternetShortcut]
2URL=http://www.cjsdn.net/index.html

        Java代码如下:
/**
 * Creator: Stephen
 * Create Time: 2010-1-27 下午02:39:26
 
*/

package com.sinosoft.tools;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.axis.utils.StringUtils;
import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.jdom.Document;
import org.jdom.Element;

import com.sinosoft.exception.SParserException;
import com.sinosoft.utility.UniversalFun;
import com.sinosoft.xml.tools.DocumentUtils;

/**
 * 解析Sogou浏览器导出的快捷方式文件,并创建快捷方式文件夹
 * 
 * 
@author Stephen
 * 
 
*/

public class PowerConverter4SE {
    
private static final Logger log = Logger.getLogger(PowerConverter4SE.class);

    
public static void main(String[] args) {
        
try {
            String filePath 
= "E:/temp/Backup20100127141922.xml";
            String outputFilePath 
= "R:/Favorites";
            Document doc 
= DocumentUtils.getDocument(filePath);
            Element root 
= doc.getRootElement();
            List itemList 
= root.getChildren("item");
            
for (int i = 0; i < itemList.size(); i++{
                Element item 
= (Element) itemList.get(i);
                parseItem(item, outputFilePath);
            }

        }
 catch (SParserException e) {
            e.printStackTrace();
        }

    }


    
/**
     * 解析节点
     * 
     * 
@param item
     *            要解析的节点
     * 
@param outputFolderPath
     *            输出文件夹路径
     
*/

    
public static void parseItem(Element item, String outputFolderPath) {
        
if (item == null{
            
return;
        }

        
if (StringUtils.isEmpty(outputFolderPath)) {
            log.error(
"The outputFolderPath is not specified!");
            
return;
        }

        
// 获取item的是否Folder属性
        String isFolder = item.getAttributeValue("IsFolder");
        String name 
= item.getAttributeValue("name");
        
if (UniversalFun.isAffirm(isFolder)) {
            
// 是文件夹
            File file = new File(outputFolderPath, name);
            
// 如果文件夹不存在,则创建文件夹
            if (!file.exists()) {
                file.mkdirs();
            }

            
// 创建子文件
            List childItemList = item.getChildren("item");
            
for (int i = 0; i < childItemList.size(); i++{
                Element childItem 
= (Element) childItemList.get(i);
                parseItem(childItem, file.getAbsolutePath());
            }

        }
 else {
            String url 
= item.getText();
            
if (StringUtils.isEmpty(url)) {
                log.debug(
"The url is empty!");
                
return;
            }

            
// 创建快捷方式
            StringBuffer fileContent = new StringBuffer();
            fileContent.append(
"[InternetShortcut]\r\nURL=");
            fileContent.append(url);
            File file 
= new File(outputFolderPath, name + ".url");
            
try {
                FileUtils.writeStringToFile(file, fileContent.toString());
            }
 catch (IOException e) {
                log.error(
"Error on write content to file!", e);
            }

        }

    }

}


posted on 2010-01-27 15:33 liwp.Stephen 阅读(928) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航: