點點滴滴

          不勞而得之財,必然消耗;勤勞積蓄的,必見加增。
          隨筆 - 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 閱讀(942) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 光山县| 鄂州市| 象山县| 海口市| 德江县| 闵行区| 青冈县| 光泽县| 保山市| 银川市| 盐山县| 宣城市| 长乐市| 瑞安市| 浙江省| 南和县| 凉山| 锦屏县| 施秉县| 利川市| 无棣县| 上杭县| 博爱县| 黎川县| 永昌县| 荔波县| 团风县| 滦平县| 弥渡县| 焉耆| 刚察县| 宜川县| 温州市| 永定县| 株洲市| 城口县| 名山县| 若羌县| 岳普湖县| 东辽县| 绩溪县|