Log in

View Full Version : Parsing and transform bookmark files



sfchun
04-15-2008, 07:19 PM
hello evryone,
I'm new in web app coding ... learning css , jsp, php and so on at the same time ^^
But i'm not efficient enough to perform what i need -_-;

Explaination :

i got a site, using , html, css frames, and jsp
i have a menu with deferent options ; one of them is : Bookmarks

This option is now reading (using ajax) a hand made flat file with URLs

this is a way to keep my bookmarks, and be able to have them from everywhen on MY site.

My Request :
Does anyone have a script, which can parse a bookmark file (URLs + comments), and then display/re-format them as a collapse tree into a DIV ???

thanks a lot for your help ^^

sfchun
04-28-2008, 08:04 AM
Maybe i was not clear ...

I have a bookmark file uploaded server side.

I need to display it as a tree menu.

I guess the process should be something like :
bookmark file -> XML file -> display as tree menu...

maybe following link can help skilled people, but for me asp is not suitable ...
http://www.15seconds.com/issue/010601.htm

Master_script_maker
04-28-2008, 12:58 PM
can you give us an example bookmark file?

sfchun
04-28-2008, 04:22 PM
well in fact you can take any bookmark file, they are all made the same.

i've tried , safari , opera, firefox and IE , all bookmarks are similar.

just export your bookmarks, as a html file
html code is something like this for example :


<!DOCTYPE NETSCAPE-Bookmark-file-1>
<!-- This is an automatically generated file.
It will be read and overwritten.
Do Not Edit! -->
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks</H1>
<DL><p>
<DT><H3 FOLDED ADD_DATE="1204377476">Links</H3>
<DL><p>
<DT><A HREF="http://go.microsoft.com/fwlink/?LinkId=53540" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">Customize Links</A>
</DL><p>
<DT><H3 FOLDED ADD_DATE="1204377476">Microsoft Websites</H3>
<DL><p>
<DT><A HREF="http://go.microsoft.com/fwlink/?LinkId=50893" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">IE Add-on site</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?linkid=44661" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">IE site on Microsoft.com</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?linkid=69151" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">Marketplace</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?linkid=55424" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">Microsoft At Home</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?linkid=68920" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">Microsoft At Work</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?linkid=68919" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">Welcome to IE7</A>
</DL><p>
<DT><H3 FOLDED ADD_DATE="1206538767">Mobile Favorites</H3>
<DL><p>
</DL><p>
<DT><H3 FOLDED ADD_DATE="1204377476">MSN Websites</H3>
<DL><p>
<DT><A HREF="http://go.microsoft.com/fwlink/?LinkId=55143" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">MSN Autos</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?LinkId=68924" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">MSN Entertainment</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?LinkId=68923" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">MSN Money</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?LinkId=68921" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">MSN Sports</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?LinkId=54729" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">MSN</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?LinkId=68922" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">MSNBC News</A>
</DL><p>
<DT><H3 FOLDED ADD_DATE="1204377476">Windows Live</H3>
<DL><p>
<DT><A HREF="http://go.microsoft.com/fwlink/?LinkId=69172" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">Get Windows Live</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?LinkId=70742" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">Windows Live Gallery</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?LinkId=68925" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">Windows Live Mail</A>
<DT><A HREF="http://go.microsoft.com/fwlink/?LinkId=68927" ADD_DATE="1204377476" LAST_VISIT="1204377476" LAST_MODIFIED="1204377477">Windows Live Spaces</A>
</DL><p>
<DT><A HREF="http://playhotcasino.com/" ADD_DATE="1205159913" LAST_VISIT="1205159913" LAST_MODIFIED="1205159913">Best Online Casino</A>
<DT><A HREF="https://www.recrutement-societegenerale.com/jpapps/france/profilemenu.jsp?0.42725871015459693" ADD_DATE="1205511934" LAST_VISIT="1205511934" LAST_MODIFIED="1205511934">Espace candidat</A>
<DT><A HREF="http://www.developertutorials.com/scripts/7/" ADD_DATE="1205513040" LAST_VISIT="1205513040" LAST_MODIFIED="1205752348">JavaScript Scripts, JavaScript Code, JavaScript Applications</A>
</DL><p>


then, watch it with any browser, and get this result :
http://img113.imageshack.us/img113/1267/bookvh9.jpg (http://imageshack.us)

and what i expect after parsing and 'rebuild' is something which looks like this (result below does not correspond to the above examples ; it just give an idea of the tree menu expected) :
http://img113.imageshack.us/img113/4024/treeoh5.jpg (http://imageshack.us)

in fact each : H3 tag should become a node of the tree
this way i gain a lot of space when displaying my favorites.

Medyman
04-28-2008, 11:07 PM
First, it can be done one of two ways...
Which one you choose is really your choice. There is definitely a cleaner way, but they both end up with the same result.

Second, I'm basing my suggestion based on this script (http://www.dynamicdrive.com/dynamicindex1/navigate1.htm) from Dynamic Drive. Other might work as well, but I haven't tested those, so can't be sure of that.

1. Javascript + CSS
First, download the script linked to above and follow the instructions on the page to get it to work. Next temporarily remove the javascript from the <head>


<script type="text/javascript" src="simpletreemenu.js">

/***********************************************
* Simple Tree Menu- © Dynamic Drive DHTML code library (www.dynamicdrive.com)
* This notice MUST stay intact for legal use
* Visit Dynamic Drive at http://www.dynamicdrive.com/ for full source code
***********************************************/

</script>

Now, add your bookmarks markup to the page. Using, CSS, style the DT and DL tags to resemble an unordered list as much as you can. The <DT> cooresponds with the <ul> tags and the <li> corresponds with the <DL> tags.

When that is done, add the javascript back in. Open the javascript file in a text editor with a find+replace feature. Use the find and replace feature to change all instances of "ul" to "DT" (including the quotes).

That should do it! You'll have a working tree menu.


2. PHP + CSS + Javascript
In this technique, you don't need to necessarily have to edit the javascript. You defnitely don't have to change the CSS (unless you want different styling).

Using PHP's str_replace() (http://us2.php.net/str_replace) function, replace all instances of <DT> to <ul> and all instances of <DL> to <li>. This will, more or less, give you an unordered list that will place nice with the DD script.

You'll need to figure out how to add the necessary CSS selectors (ids and classes) to the first ul tag and to remove that extraneous text in there...but that's nothing a little PHP regex can't handle.


Other than these two methods, you could into some XML parsing scripts for some ideas.

Hope that helps.

sfchun
04-29-2008, 06:46 AM
Hi , thanks for your answer !

I'm a noob web coder =P but when i read you, i realized i was maybe thinking too complicated ... :p

Anyway after fighting with the bookmark syntax 'logic ?', i succed to do something !

here is the PHP code bellow (assuming all files (js, css, php, html, gif) are in same directory) :


<?php

// DISPLAY/SAVE YOUR (ALREADY) UPLOADED BOOKMARKS AS TREE MENU.

// EDIT BELLOW !
$originalBookmarkFile = 'bookmarks.html'; // name of the bookmark file to display as a tree
$displayResult = 'yes'; // change to : no , if you don't want to display the result (yes , is default)
$saveNewFile = 'no'; // change to yes to save the modified bookmark file (no, is default)
$fileToSave = 'booktree.html'; // name the file to be saved

// NO EDIT NEEDED AFTER THIS LINE !

// get the file content ina string
$fh = @fopen("$originalBookmarkFile", "r") or die("can't open file");
$file = @file_get_contents("$originalBookmarkFile");
@fclose($fh);

// work on string to make it becomes an html list (and bookmark type as classic page type)
$endPage = "\n</BODY>";
$file = str_replace('<META HTTP-EQUIV=',"<HEAD>\n<META HTTP-EQUIV=",$file);
$file = str_replace('</TITLE>',"</TITLE>\n</HEAD>\n<BODY>",$file);
$file = str_replace('<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">','',$file); // fix other browser than IE
$file= str_replace('<DD>Add bookmarks to this folder to see them displayed on the Bookmarks Toolbar','',$file); // remove firefox comments

$file = str_replace("</H1>","</H1>\n<ul id=\"treemenu1\" class=\"treeview\">\n<li><H3>Click to Browse Bookmark Tree</H3>",$file);
$file = str_replace('<DT><H3',"<li><H3",$file);
$file = str_replace('<DT><A HREF=','<li><A HREF=',$file);
$file = str_replace('</A>','</A></li>',$file);
$file = str_replace('<DL><p>','<ul>',$file);
$file = str_replace('</DL><p>',"</ul>\n</li>",$file);
$file = str_replace('<DD>',"''",$file);

$file = $file.$endPage;

// Adding simplemenu.css
$file = str_replace('</HEAD>',"<link rel=\"stylesheet\" type=\"text/css\" href=\"simpletree.css\" />\n</HEAD>",$file);

// Adding simplemenutree.js
$file = str_replace('</HEAD>',"<script language=\"JavaScript\" src=\"simpletreemenu.js\" type=\"text/javascript\"></script>\n</HEAD>",$file);
$file = str_replace('</BODY>',"</ul>\n<script type=\"text/javascript\">ddtreemenu.createTree(\"treemenu1\", 'contract')</script>\n</BODY>",$file);

// display result
if ($displayResult != 'no'){
echo $file;
}

// save the new bookmark file
if ($saveNewFile == 'yes'){
$fileToSaveHandle = @fopen($fileToSave, 'w') or die("can't open file");
@fclose($fileToSaveHandle);

$Handle = @fopen($fileToSave, 'a');
@fwrite($Handle, $file);
@fclose($Handle);
}
?>


Sorry in advance for the 'basic' code, but i do with what i know =P
If someone can post a simplified or more efficient code, feel free =)

Thanks again Medyman for your post !
:)

sfchun
04-29-2008, 07:46 AM
arf i was too fast !!
it works on IE, but have trouble in firefox, opera and safari

i fixed it !
see red line in above code.

sfchun
04-29-2008, 08:12 AM
Still have a little bug !

Directory picutres (closed.gif, open.gif), are not displayed on first load.
But once i've clicked on one its icons are seens.

Any ideas ??? :confused:

sfchun
04-29-2008, 08:17 AM
Sorry ! my mistake ! :rolleyes:

... wrong path in css file ... :D

vaishali13
03-02-2011, 07:00 AM
I used this code to display the favorites in tree format...
but it doesn't give me any output...:confused:

I am new to programming..

I want to fetch the users all bookmarks stored on local drive of all browsers by using php..

how can I do it ????

Please help me soon.. its urgent:(

Thanks
Vaishali