#!/usr/bin/perl -w
use strict;

use XML::Parser;

my $skipped = 0;
my $open = 0;
my $content = "";
my $uri = "";

sub handle_start($$@) {
   my ($expat, $element, @attrs) = @_;
   if ($skipped == 1) {
      $uri = $attrs[1];
      $uri =~ s/'/''/g;
   }
   if ($skipped < 2) { $skipped++ ; }
   else {
      $open++;
      $content = "";
   }
}

sub handle_end($$) {
   my ($expat,$element) = @_;
   $open--;
   if ($open >= 0) {
      $content =~ s/'/''/g;
      $element =~ s/(\w+):(\w+)/$1$2/;
      my $query = "INSERT INTO $element VALUES ('$content','$uri');";
			print $query, "\n";
   }
}

sub handle_char($$) {
   my ($expat,$char) = @_;
   if ($open >= 1) {
      chomp($char);
      $char =~ s/ +/ /g;
      $char =~ s/^ //g;
      $char =~ s/ $//g;
      $content .= $char;
   }
}

my $p = new XML::Parser(
  Handlers =>
    { Start => \&handle_start,
      End   => \&handle_end,
      Char  => \&handle_char});

$p->parsefile($ARGV[0]);

