The XML::Simple module provides a simple API layer on top of an underlying XML parsing module.
Install XML::Simple module using cpan or ppm utilities. After Installation process completed, check for ParserDetails.ini file (For eg C:\Perl\site\lib\XML\SAX folder) if file is not present then create a new file (ParserDetails.ini) and add below mentioned two lines.
[XML::SAX::PurePerl]
http://xml.org/sax/features/namespaces = 1
#===========================#
Sample Xml File : sample_sales.xml
#-----Perl Program-----#
# use module
#Easy API to maintain XML
use XML::Simple;
use DBI;
# declare variable
my $Xml_File_Path = "c:\\perlprog\\sample_sales.xml";
# Check file exist and not empty
if ( -z $Xml_File_Path) {
print "File Doesn't Exist!";
exit 1;
}
# create XML::Simple parser object
my $xml = new XML::Simple;
# read XML file
#XMLin() accepts an optional XML specifier followed by zero or more 'name => value' option pairs.
#Parses XML formatted data and returns a reference to a data structure which contains the same #information in a more readily accessible form.
# ForceArray = force the contents of all elements to be parsed as arrays instead of hashes.
# KeyAttr = which applies to XMLin() and XMLout() to name attributes, or sub-element
#as keys to be used to promote the parent element from array to hash.
#Remember that there is default list: "name", "key", and "id".
my $Doc = $xml->XMLin( $Xml_File_Path,
ForceArray => 1,
KeyAttr => {},
);
# create connection with Char database (Chart is data source name)
my $dbh = DBI->connect('dbi:Chart:') or die "Cannot connect\n";
# Once connection is established, we want to create bar graph so create bar table with required fields
$dbh->do('CREATE TABLE bars (quarter SMALLINT, East FLOAT, Southeast FLOAT, Midwest FLOAT, Southwest FLOAT, Northwest FLOAT)');
my $sth = $dbh->prepare('INSERT INTO bars VALUES(?, ?, ?, ?, ?, ?)');
# read xml data and and insert it into bar table
foreach my $sales (@{$Doc->{sales}}) {
# If you want to check output on command prompt else comment below mentioned lines
print $sales->{quarter}->[0], "\n";
print $sales->{East}->[0], "\n";
print $sales->{Southeast}->[0], "\n";
print $sales->{Midwest}->[0], "\n";
print $sales->{Southwest}->[0], "\n";
print $sales->{Northwest}->[0], "\n";
$sth->execute($sales->{quarter}->[0],$sales->{East}->[0],$sales->{Southeast}->[0],$sales->{Midwest}->[0],$sales->{Southwest}->[0],$sales->{Northwest}->[0]);
}
$sth->finish(); # free up resources
# chart Width, Height
# chat x axis field name (X_AXIS) and y axis field name (Y_AXIS) (we have two fields year and amount)
# TITLE (Title of Chart)
# COLOR (represent Color of line chart), SHOWPOINTS (if 1 display points on Line chart)
#SHOWVALUES=1 (if 1 display the value of points)
#THREE_D=1 (if 1, sets the 3-D effect on barchart)
#SIGNATURE='Copyright(C) 2010, LampSys'");
$rsth = $dbh->prepare( "SELECT BARCHART FROM bars WHERE WIDTH=700 AND HEIGHT=500 AND X_AXIS='Quarter' AND Y_AXIS='Revenue' AND TITLE = 'Quarterly Revenue By Region' AND THREE_D=1 AND SHOWVALUES=1 AND COLORS IN ('red', 'green', 'blue', 'yellow', 'dbrown') AND SIGNATURE='Copyright(C) 2011, LampSys'");
$rsth->execute;
#Binds a variable ($buf) with attributes (output columns) of a SELECT statement.
$rsth->bind_col(1, \$buf);
$rsth->fetch;
$rsth->finish(); #free up resources
#open a file (as PNG, JPEG, GIF) in binary mode
open(OUTF, '>Sales.png');
binmode OUTF;
print OUTF $buf;
close(OUTF);
#disconnect Database connection
$dbh->disconnect;
#-----------------OutPut----------------------#