Click here to Skip to main content
11,415,074 members (65,594 online)
Click here to Skip to main content

How to Parse the Bookmarks File of Mozilla into a Datatable

, 14 Sep 2007
Rate this:
Please Sign up or sign in to vote.
An article describing how to parse a Mozilla bookmarks file into a datatable
Screenshot - ImportMozilla1.jpg

Introduction

In this article, I create a procedure to import the file HTML containing the favourites of Mozilla Firefox. It is useful for parsing the file, putting it into a DataTable and then putting it into a TreeView. You can import Mozilla Firefox favourites to your application with this class.

Using the Code

Essentially, the file is an HTML file with particular tags to identify the URLs and folders. When you meet the tag <DT> followed by <H3>, it means that a new folder starts. I used a static method from the class ClsMozilla called ReadBookmark that returns a DataTable, accepting as a parameter the physical name of the file containing the bookmarks. I used StreamReader StreamBookMarks to make a cycle in order to analyze all the rows contained in the file. When I met the relevant tag, I placed the Folder and Links in the DataTable.

int Last_parent_id = 0;
//this is the relation with a folder. 0 means that it is alone
int Before_parent_id = 0;
//when close the folder, this store the prev id

This is done in order to associate each link or folder with a parent folder. This is fundamental when you want to build the tree of the links because you need to know the parent. This is interesting use of the DataTable because, in the first round, I tried to use a multidimensional array, but the management of this last is really more difficult than a DataTable. In the DataTable, I created various columns.

DataTable dt = new DataTable();
dt.Columns.Add( new DataColumn("id", typeof(Int32)));
dt.Columns.Add( new DataColumn("parent_id", typeof(Int32)));
dt.Columns.Add( new DataColumn("Hierarchy", typeof(Int32)));
dt.Columns.Add(new DataColumn("Title", typeof(string)));
dt.Columns.Add(new DataColumn("Link", typeof(string)));
dt.Columns.Add(new DataColumn("Des", typeof(string)));
dt.Columns.Add(new DataColumn("Type", typeof(string)));
DataRow dr ;

In the variable Type, I trace the nature of the link, i.e. if it is a URL or a Folder. I used a simple Regex to remove the HTML code.

public static string StripHTMLTags(string StrText)
{
//On retire le code HTML
return Regex.Replace(StrText,@"<(.|\n)*?>",string.Empty);
 }

Points of Interest

The class is very simple, as is the Firefox file's structure. The favourites of Internet Explorer are simpler because every favourite is a simple file. The Opera bookmark file is similar to Firefox and, in the future, I will release a new article for the other browser.

History

  • 14 September, 2007 -- Original version posted

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Pierpaolo Romanelli
Web Developer therealbest.com
Italy Italy
I am a professional developer in Milan, I wrote many software and site for firm and bank, I like programming because I think it is an art!
My site is http://www.therealbest.com and http://www.rentvillasin.com

Comments and Discussions

 
QuestionAwesome! Pin
thund3rstruck2-Nov-14 9:31
memberthund3rstruck2-Nov-14 9:31 
AnswerRe: Awesome! Pin
Pierpaolo Romanelli24-Apr-15 9:59
memberPierpaolo Romanelli24-Apr-15 9:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150427.4 | Last Updated 14 Sep 2007
Article Copyright 2007 by Pierpaolo Romanelli
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid