Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Comment on

( #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":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

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

    How do I use this? | Other CB clients
    Other Users?
    Others surveying the Monastery: (10)
    As of 2014-11-24 16:42 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (143 votes), past polls