Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
#!/usr/bin/perl -w use strict; use XML::Writer; # Small utility to parse Opera bookmark files. # Originally written in Python by Lars Marius Garshol # perl version - Briac Pilpré my $version = <>; die "Doesn't seem to be a Opera Hotlist\n" if $version !~ /^Opera/; chomp( $version ); my $xml = new XML::Writer(DATA_MODE=>1, DATA_INDENT=>1); $xml->xmlDecl(); $xml->doctype( 'xbel', '+//IDN python.org//DTD XML Bookmark Exchange Language 1.0//EN/ +/XML', 'xbel.dtd'); $xml->startTag('xbel'); $xml->dataElement('desc', $version); while (<>){ next unless /\S/; chomp; if ( $_ eq '#FOLDER' || $_ eq '#URL' ){ my $node = $_; my $line; { local $/ = ""; # Paragraph mode $line = <>; } if ($node eq '#FOLDER'){ my $name = $1 if $line =~ /NAME=(.*)/; my $created = $1 if $line =~ /CREATED=(.*)/; $xml->startTag('folder'); $xml->dataElement('title', $name); } elsif ($node eq '#URL') { my $name = $1 if $line =~ /NAME=(.*)/; my $created = $1 if $line =~ /CREATED=(.*)/; my $order = $1 if $line =~ /ORDER=(.*)/; my $visited = $1 if $line =~ /VISITED=(.*)/; my $url = $1 if $line =~ /URL=(.*)/; $xml->startTag('bookmark', href => $url, added => $created, visited => $visited); $xml->dataElement('title', $name); $xml->endTag('bookmark'); } } elsif ( $_ eq '-'){ $xml->endTag('folder'); } } $xml->endTag('xbel');

Bonus: Added Bonus the xbel2adr script!

__END__ #!/usr/bin/perl -w use strict; use XML::Parser; die "usage: $0 opera.xbel\n" if !$ARGV[0]; my $parser = new XML::Parser(Handlers => { Start=>\&start, End=>\&end, Char=>\&char, }); my%equiv=(title=>'NAME', desc=>'DESCRIPTION'); my $flag = ''; $parser->parsefile($ARGV[0]); sub char { my ($p, $txt) = @_; return if $txt =~ /^\s*$/; chomp($txt); print $txt . "\n" if exists($equiv{$flag}); } sub start { my ($p, $e, %a) = @_; $flag = $e; if ($e eq 'folder'){ print "\n#FOLDER\n" } elsif ($e eq 'bookmark'){print "\n#URL\n\tURL=" . $a{'href'} if $a +{'href'}} else {print "\n\t" . $equiv{$e} . "=" if exists $equiv{$e} } } sub end {undef $flag}

Previous version:

__END__ #!/usr/bin/perl -w # # adr2xbel : Convert Opera Bookmarks file to xbel # (XML Bookmark Exchange Language) # OeufMayo - 23 apr 2001 use strict; use XML::Writer; use IO; die "usage: $0 opera.adr\n" if !$ARGV[0]; my $output = new IO::File("> $ARGV[0].xbel.xml") or die $!; my $writer = new XML::Writer(OUTPUT => $output, DATA_MODE => 1, DATA_INDENT => 4) or die $!; $writer->xmlDecl('iso-8859-1', 'no'); $writer->doctype( 'xbel', undef, 'http://www.python.org/topics/xml/dtds/xbel-1.0.dtd'); $writer->startTag('xbel'); $writer->dataElement('title', 'Opera Hotlist version 2.0'); $/ = ""; while (<>){ chomp; my @chunk = split(/\n\t/, $_); while ($chunk[0] =~ s/^(-\n)//){ # Folder end $writer->endTag('folder') if $chunk[0]; } if ($chunk[0] =~ /^#FOLDER/){ # Folder beginning shift @chunk; my %attrs = &getAttlist(@chunk); $writer->startTag("folder"); for ('title', 'desc'){ $writer->dataElement($_, $attrs{$_}); } } elsif ($chunk[0] =~ /^#URL/){ # URL Tag shift @chunk; my %attrs = &getAttlist(@chunk); $writer->startTag("bookmark", 'href' => $attrs{href} ); for ('title', 'desc'){ $writer->dataElement($_, $attrs{$_}); } $writer->endTag("bookmark"); } } $writer->endTag('xbel'); $writer->end; ######################### sub getAttlist { my %attrs; my %tr = ( URL=>'href', DESCRIPTION=>'desc', NAME=>'title', ); foreach $_ (@_){ m/^([^=]+)=(.*)$/; my ($key, $value) = ($1, $2); next unless ( $key eq 'URL' || $key eq 'DESCRIPTION' || $key eq 'NAME' ); $key = $tr{$key}; $attrs{$key} = $value; } return %attrs; }

In reply to Opera Bookmarks to XBEL by OeufMayo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2024-04-18 03:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found