上一篇介绍了Chrome书签和IE收藏夹文件的格式对应,需要先将Chrome书签文件格式转换为XML方便操作。本篇就将如何进行这种转换。
考虑到文件转换效率,一行行读取后判断在转换的方式固然是最简单思路最清晰地,但是效率恐怕是最低下的。如果遇到收藏加层次很多文件很多的情况就很慢,因此我们选择用正则表达式替换方式对全文依次按照指定模式替换相应内容,最后生成需要的XML文件。
这里列出我在程序中使用的正则表达式:
"\\s*\"other\".*\\n.*\\n.*\\ {"
".*\\n\\s*\"date_added\".*\\n.*\\n.*\\n.*\\n.*\\n.*\\n.*\\n.*\\\\u5176\\\\u4ED6\\\\u4E66\\\\u7B7E.*\\n.*"
"\\s*\"bookmark_bar\":\\s\\ {"
"\\s*\"children\":\\s\\[\\s\\ {"
"\\s*\"date_added\":\\s\"(.*)\ ","
"\\s*\"date_modified\":\\s\"(.*)\ ","
"\\],"
"\\{|\\ }|,"
"(\\\\u ....)"
对照源程序可以看得懂是用来替换什么部分的。值得一提的是在C#的正则表达式替换部分增加一个处理事件,可以同事将Unicode编码的名字转换为中文。
- private string Unicode2GB(Match regmatch)
- {
- string dst = "" ;
- string src = regmatch.Value;
- string str = "" ;
- str = src.Substring(2);
- byte [] bytes = new byte [2];
- bytes[1] = byte .Parse( int .Parse(str.Substring(0, 2), NumberStyles.HexNumber).ToString());
- bytes[0] = byte .Parse( int .Parse(str.Substring(2, 2), NumberStyles.HexNumber).ToString());
- dst = Encoding.Unicode.GetString(bytes);
- return dst;
- }
最后就是导出XML格式为IE可用的收藏夹了,这部分简单,一个递归函数分析XML中的标记,然后在给定的路径创建文件夹或.url文件即可。
最后贴出所有的源码,大家可以参考改进,祝大家顺利!
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using System.Text.RegularExpressions;
- using System.Globalization;
- using System.Xml;
- namespace BookmarksTrans
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- private void button1_Click( object sender, EventArgs e)
- {
- openFileDialog1.ShowDialog();
- if (openFileDialog1.FileName!= "" )
- {
- txt_Chrome.Text = openFileDialog1.FileName;
- }
- }
- private void DealBookMark( string bookmarkFile)
- {
- XmlDocument xmlDoc = new XmlDocument();
- XmlNode xmlBase;
- XmlNodeList xmlURLlist;
- StreamReader sdr = new StreamReader(bookmarkFile);
- TreeNode tempNode = new TreeNode();
- string readData = "" ;
- try
- {
- // 处理Chrome收藏夹格式为xml
- readData = sdr.ReadToEnd();
- readData = Regex.Replace(readData, "\\s*\"other\".*\\n.*\\n.*\\{" , "" );
- readData = Regex.Replace(readData,
- ".*\\n\\s*\"date_added\".*\\n.*\\n.*\\n.*\\n.*\\n.*\\n.*\\n.*\\\\u5176\\\\u4ED6\\\\u4E66\\\\u7B7E.*\\n.*" ,
- "" );
- readData = Regex.Replace(readData, "&" , "@" );
- readData = Regex.Replace(readData, "\\s*\"roots\":\\s\\{" ,
- "<?xml version=\"1.0\" encoding=\"GBK\" ?>" );
- readData = Regex.Replace(readData, "\\s*\"bookmark_bar\":\\s\\{" , "<bookmark>" );
- readData = Regex.Replace(readData, "\\s*\"children\":\\s\\[\\s\\{" , "<bookmarkchild>" );
- readData = Regex.Replace(readData, "\\s*\"date_added\":\\s\"(.*)\"," ,
- "<date_added>$1</date_added>" );
- readData = Regex.Replace(readData, "\\s*\"name\":\\s\"(.*)\"," , "<name>$1</name>" );
- readData = Regex.Replace(readData, "\\s*\"type\":\\s\"(.*)\"" , "<type>$1</type>" );
- readData = Regex.Replace(readData, "\\s*\"url\":\\s\"(.*)\"" , "<url>$1</url>" );
- readData = Regex.Replace(readData, "\\s*\"date_modified\":\\s\"(.*)\"," ,
- "<date_modified>$1</date_modified>" );
- readData = Regex.Replace(readData, "\\s*\"other\":\\s\\{" , "<bookmarkchild>" );
- readData = Regex.Replace(readData, "\\]," , "</bookmarkchild>" );
- readData = Regex.Replace(readData, "\\{|\\}|," , "" );
- readData = Regex.Replace(readData, "\\s*\"version\":\\s1" , "</bookmark>" );
- readData = Regex.Replace(readData, "(\\\\u....)" , new MatchEvaluator(Unicode2GB));
- // 导出收藏夹
- xmlDoc.LoadXml(readData);
- xmlBase = xmlDoc.DocumentElement;
- xmlURLlist = xmlBase.ChildNodes;
- tempNode.Text = "收藏夹" ;
- trv_MarkbookList.Nodes.Add(tempNode);
- AddBookMark(xmlURLlist,tempNode);
- }
- catch (Exception ex)
- {
- MessageBox.Show( "书签分析出错:" +ex.Message, "错误" , MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- finally
- {
- sdr.Close();
- sdr.Dispose();
- }
- }
- /// <summary>
- /// Unicode编码转换为汉字
- /// </summary>
- /// <param name="regmatch"></param>
- /// <returns></returns>
- private string Unicode2GB(Match regmatch)
- {
- string dst = "" ;
- string src = regmatch.Value;
- string str = "" ;
- str = src.Substring(2);
- byte [] bytes = new byte [2];
- bytes[1] = byte .Parse( int .Parse(str.Substring(0, 2), NumberStyles.HexNumber).ToString());
- bytes[0] = byte .Parse( int .Parse(str.Substring(2, 2), NumberStyles.HexNumber).ToString());
- dst = Encoding.Unicode.GetString(bytes);
- return dst;
- }
- /// <summary>
- /// 将具有子栏目的书签添加到TreeView中
- /// </summary>
- /// <param name="xmlList">子栏目</param>
- private void AddBookMark(XmlNodeList xmlList,TreeNode parentNode)
- {
- TreeNode PNode = parentNode;
- for ( int i = 0; i < xmlList.Count - 1; i++)
- {
- if (xmlList[i].Name == "bookmarkchild" )
- {
- TreeNode sonNode = new TreeNode();
- if (xmlList[i+3].InnerText != "书签栏" )
- {
- sonNode.Text = xmlList[i + 3].InnerText;
- parentNode.Nodes.Add(sonNode);
- PNode = sonNode;
- }
- AddBookMark(xmlList[i].ChildNodes, PNode);
- }
- else if (xmlList[i].Name == "type" && xmlList[i].InnerText == "url" )
- {
- // 文件名非法字符处理
- string urlName=xmlList[i - 1].InnerText.Replace( "." , "." ).Replace( "\\"," \");
- urlName=urlName.Replace( "/" , "/" ).Replace( "<" , "<" );
- urlName = urlName.Replace( ">" , ">" ).Replace( "*" , "*" ).Replace( "?" , "?" );
- urlName = urlName.Replace( ":" , ":" ).Replace( "\"" , """ ).Replace( "|" , "|" );
- TreeNode urlNode= new TreeNode(urlName);
- urlNode.Tag = xmlList[i + 1].InnerText;
- parentNode.Nodes.Add(urlNode);
- }
- }
- }
- private void btn_DealChrome_Click( object sender, EventArgs e)
- {
- if (txt_Chrome.Text.Trim()== "" )
- {
- MessageBox.Show( "不给出Chrome书签位置,我也帮不了你!" , "不耐烦" );
- return ;
- }
- DealBookMark(txt_Chrome.Text);
- }
- private void btn_InIE_Click( object sender, EventArgs e)
- {
- string iebookmarkPath = "" ;
- if (txt_IEbookmark.Text!= "" )
- {
- iebookmarkPath = txt_IEbookmark.Text;
- }
- else
- {
- iebookmarkPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites);
- }
- DirectoryInfo favorite = Directory.CreateDirectory(iebookmarkPath);
- ExportBookMarks(trv_MarkbookList.Nodes,favorite);
- MessageBox.Show( "成功完成书签导出" );
- }
- /// <summary>
- /// 导出书签到指定文件夹
- /// </summary>
- /// <param name="folderpath"></param>
- private void ExportBookMarks(TreeNodeCollection Nodes,DirectoryInfo foldInfo)
- {
- for ( int i = 0; i < Nodes.Count; i++)
- {
- if (Nodes[i].Nodes.Count>0)
- {
- DirectoryInfo fold =foldInfo.CreateSubdirectory(Nodes[i].Text);
- ExportBookMarks(Nodes[i].Nodes,fold);
- }
- else
- {
- FileStream fileSW = File.Open(foldInfo.FullName + "\\" + Nodes[i].Text + " .url",FileMode.Create);
- StreamWriter stWriter = new StreamWriter(fileSW);
- try
- {
- stWriter.WriteLine( "[InternetShortcut]" );
- stWriter.WriteLine( "URL=" + Nodes[i].Tag.ToString());
- stWriter.Flush();
- }
- catch (Exception ex)
- {
- MessageBox.Show( "导出书签出错:" + ex.Message, "错误" , MessageBoxButtons.OK, MessageBoxIcon.Error);
- }
- finally
- {
- stWriter.Close();
- stWriter.Dispose();
- fileSW.Close();
- fileSW.Dispose();
- }
- }
- }
- }
- private void btn_IE_Click( object sender, EventArgs e)
- {
- folderBrowserDialog1.ShowDialog();
- if (folderBrowserDialog1.SelectedPath!= "" )
- {
- txt_IEbookmark.Text = folderBrowserDialog1.SelectedPath;
- }
- }
- }
- }
附工程文件的下载链接:http://www.rayfile.com/files/27021328-b4b5-11dd-b869-0019d11a795f/