Hello,

I'm trying to process the bookmarks.xml file created by the Chimera
webbrowser. If you have Chimera, this file is at
~/Library/Application Support/Chimera/Profiles.

What I want is to create a very simple html page from bookmarks.xml.
I'm using XML::Twig for that, but I have found a strange behavior and
after perusing the documentation for
XML::Twig<http://www.xmltwig.com/>, I didn't see an answer. I know
that I could contact the author, but I rather check here first.

==========================================
Here's an example of a bookmarks.xml file:
==========================================

<bookmarks xmlns="http://chimera.mozdev.org/bookmarks/";>
  <folder name="Toolbar Bookmarks" type="toolbar" open="true">
    <bookmark name="Chimera"
href="http://chimera.mozdev.org/installation.html"; />
    <bookmark name="Mozilla.Org" href="http://www.mozilla.org"; />
    <folder name="News" open="true">
      <bookmark name="Daily Daemon News" href="http://daily.daemonnews.org/"; />
      <bookmark name="Cyberpresse" href="http://www.cyberpresse.ca/"; />
      <bookmark name="CNN" href="http://www.cnn.com"; />
    </folder>
  </folder>
  <folder name="OS X" open="true">
    <bookmark name="OSXFAQ" href="http://www.osxfaq.com/"; />
  </folder>
</bookmarks>

==========================================
Here's my script:
==========================================

use strict;
use XML::Twig;

my $fbookmarks = '/Users/username/Desktop/bookmarks.xml';
my $xmlheader  = qq/<?xml version="1.0" encoding="ISO-8859-1"?>\n/;

my $bookmarks;
open (FH, $fbookmarks);
{ local $/;  $bookmarks = <FH>; }
close (FH);
$bookmarks = $xmlheader . $bookmarks;

my $twig = XML::Twig->new(  KeepEncoding => 1,
                            TwigHandlers => {
                              'folder'  => \&formatfolder,
                              'bookmark'  => \&formatbookmark } );

$twig->parse($bookmarks);
$twig->purge;

my $s;
sub formatfolder {
  my ($twig, $elem) = @_;
  $s .= "<h4>" . $elem->att("name") . "</h4>\n";
}

sub formatbookmark {
  my ($twig, $elem) = @_;
  $s .= "<a href='" . $elem->att("href") . "'>" . $elem->att("name")
. "</a><br>\n";
}

print "<htm><body>\n$s\n</body></html>";

==========================================
What I'd like to get is this output:
==========================================

<htm><body>
<h4>Toolbar Bookmarks</h4>
<a href='http://chimera.mozdev.org/installation.html'>Chimera</a><br>
<a href='http://www.mozilla.org'>Mozilla.Org</a><br>
<h4>News</h4>
<a href='http://daily.daemonnews.org/'>Daily Daemon News</a><br>
<a href='http://www.cyberpresse.ca/'>Cyberpresse</a><br>
<a href='http://www.cnn.com'>CNN</a><br>
<h4>OS X</h4>
<a href='http://www.osxfaq.com/'>OSXFAQ</a><br>
</body></html>

==========================================
But I get  this instead:
==========================================

<htm><body>
<a href='http://chimera.mozdev.org/installation.html'>Chimera</a><br>
<a href='http://www.mozilla.org'>Mozilla.Org</a><br>
<a href='http://daily.daemonnews.org/'>Daily Daemon News</a><br>
<a href='http://www.cyberpresse.ca/'>Cyberpresse</a><br>
<a href='http://www.cnn.com'>CNN</a><br>
<h4>News</h4>
<h4>Toolbar Bookmarks</h4>
<a href='http://www.osxfaq.com/'>OSXFAQ</a><br>
<h4>OS X</h4>
</body></html>
==========================================

As you see, it look like XML::Twig is processing from inward to
outward, so all the bookmark nodes are processed before the folder
nodes, so the handler formatbookmark is called before formatfolder.

I'm surprise by this behavior, but maybe I'm missing something here.

TIA

Cheers
-Emmanuel

--
______________________________________________________________________
Emmanuel D�carie / Programmation pour le Web - Programming for the Web
Frontier - Perl - PHP - Javascript - XML  <http://scriptdigital.com/>

Reply via email to