package MIS_SAX_Parser;
use Data::Dumper;
use XML::SAX::Base;
@ISA = ('XML::SAX::Base');
use strict;
use constant TYPE0 => 0;
use constant TYPE1 => 1;
use constant TYPE2 => 2;
sub new {
my ($class, $callback) = @_;
my $self = { COUNTER => 0, HASHTYPE => TYPE0 };
bless $self, $class;
if ($callback) {
$self->{CALLBACK} = \&$callback;
} else {
$self->{CALLBACK} = \&nocall;
}
return $self;
}
sub start_element {
my ($self, $data) = @_;
if ($data->{Name} eq "Column") {
$self->{COLUMN_DATA}->{$data->{Attributes}->{"{}name"}->{Value}} = $data->{Attributes}->{"{}value"}->{Value};
} elsif ($data->{Name} eq "Table") {
$self->{TABLE_NAME} = $data->{Attributes}->{"{}name"}->{Value};
}
}
sub end_element {
my ($self, $data) = @_;
my $name = $data->{Name};
if ($name eq "Table") {
$self->{CALLBACK}->($self->{TABLE_NAME}, $self->{COLUMN_DATA});
# Reset the TABLE_NAME and COLUMN_DATA
$self->{TABLE_NAME} = undef;
$self->{COLUMN_DATA} = {};
}
}
# Debug callback function
sub nocall {
my ($table, $data) = @_;
print "Table name is $table\n";
print Dumper($data)."\n\n";
}
1;
####
package MIS_PrintStatus;
use Data::Dumper;
use strict;
use warnings;
sub new {
my ($class) = @_;
my $self = {};
bless $self, $class;
return $self;
}
sub printit {
my ($table, $data) = @_;
print "The table name is $table\n";
}
1;
####
#!/usr/bin/perl
use XML::LibXML;
use XML::LibXML::SAX::Parser;
use MIS_SAX_Parser;
use MIS_PrintStatus;
use strict;
my $filename = shift @ARGV;
my $cb = MIS_PrintStatus->new();
my $parser = XML::LibXML->new;
my $doc = $parser->parse_file($filename);
my $handler = MIS_SAX_Parser->new(\&MIS_PrintStatus::printit);
my $generator = XML::LibXML::SAX::Parser->new(Handler => $handler);
$generator->generate($doc);