I have different modules like Author.pm, BillingPeriod.pm, Offer.pm, PaymentMethod.pm, etc. now in sax whenever I click on the end element tag, I want to create a module object that is equivalent to the element value.
How can i achieve this?
For example, if parsing an XML file and a parser parser element is the same as it should create an Offer.pm object, similarly if a parser parser loses an end element than it should create an Author.pm object
code
XML: books.xml
<?xml version="1.0" encoding="UTF-8"?>
<bks:books xsi:schemaLocation="urn:books Untitled1.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:bks="urn:books">
<book id="String">
<author>String</author>
<authorFirstName>String</authorFirstName>
<authorLastName>String</authorLastName>
<title>String</title>
<titleNo>3</titleNo>
<genre>String</genre>
<offer>String</offer>
<pub_date>1967-08-13</pub_date>
<review>String</review>
<reviewsratings></reviewratings>
</book>
</bks:books>
sax: perlsaxparsing.pl
use XML::SAX::ParserFactory;
use MySaxHandler;
my $handler = MySaxHandler->new();
my $parser = XML::SAX::ParserFactory->parser(Handler => $handler);
$parser->parse_uri("books.xml")
For example, in the example below, assuming sax clicks Offer end element tag, I create an objectOffer.pm
, , Offer.pm, , sax Offer.
package Offer;
use strict;
sub new {
my $class = shift;
my $self = {
_objectId => shift,
_price => shift
};
bless $self, $class;
return $self;
}
sub getObjectId {
my ($self) = @_;
return $self->{_objectId};
}
sub getprice {
my ($self) = @_;
return $self->{_price};
}
sub doPreInsetCheck() {
my ($self) = @_;
my %refTable;
if ( defined $self->getObjectId == 1 ) {
$refTable{'object_id'} = $self->getObjectId;
}
if ( defined $self->getprice == 1 ) {
$refTable{'fk2_price'} = $self->getprice;
}
return %refTable;
}
sub getSQLScript {
my $tableName = 'product_offer';
my ($self) = @_;
my $sqlOutput = "Insert into " . $tableName . "(";
my %refTable = $self->doPreInsetCheck();
my @colNames = keys %refTable;
my $ctr;
foreach ( $ctr = 0 ; $ctr < ( $#colNames + 1 ) ; $ctr++ ) {
$sqlOutput .= $colNames[$ctr];
if ( $ctr < $#colNames ) {
$sqlOutput .= ",";
}
}
$sqlOutput .= ") values (";
my @colVals = values %refTable;
foreach ( $ctr = 0 ; $ctr < ( $#colVals + 1 ) ; $ctr++ ) {
$sqlOutput .= $colVals[$ctr];
if ( $ctr < $#colVals ) {
$sqlOutput .= ",";
}
}
$sqlOutput .= ");";
return $sqlOutput;
}
1;
SAX Parser Handler Module: MySaxHander.pm
sub end_element {
my($self,$data) = @_;
print "\t Ending element:".$data->{Name}."\n";
my $obj = new Price("1","2","NL","ENUM","DESCRIPTION","2008-01-01 10:00:00","2009-01-01 10:00:00","2008-01-01 10:00:00","USER");
print $obj->getSQLScript."\n";
$in_books--;
}
: XML SAX, , ?