I have issue with XML Parsing. The following is the xml content.
<story>
<page>
<author>Author 1 </author>
<keywords>Keyword1 </keywords>
<headline>Headline1 </headline>
<image> Image1 </image>
<description>Desc 1 </description>
</page>
<page>
<author>Author 2</author>
<keywords>Keyword2 </keywords>
<headline>Headline 2 </headline>
<image> Image2 </image>
<description>Decs 2 </description>
</page>
</story>
I need array of hash references. Means the above xml will produce two records in hash format. The two hash references will be pushed to array.
I have written the below parser. But the result render only one record. The second record is empty. Please suggest.
#!/home/y/bin/perl5.8.5 -w
use XML::Parser;
use strict;
use Data::Dumper;
my $parser = new XML::Parser(ErrorContext => 2);
my $xmlStr = "";
my $pageFlag =0 ;
my $authorFlag = 0;
my $titleFlag = 0;
my $descFlag = 0;
my $headlineFlag = 0;
my $imageFlag = 0 ;
my $keywordFlag = 0;
my @pagesArray = ();
my %hash = ();
my $pageEnd = 0 ;
my $authorTxt = '';
my $keywordTxt = '';
my $headlineTxt = '';
my $imageTxt ='';
my $descTxt = '';
$xmlStr = "
<story>
<page>
<author>Author 1 </author>
<keywords>Keyword1 </keywords>
<headline>Headline1 </headline>
<image> Image1 </image>
<description>Desc 1 </description>
</page>
<page>
<author>Author 2</author>
<keywords>Keyword2 </keywords>
<headline>Headline 2 </headline>
<image> Image2 </image>
<description>Decs 2 </description>
</page>
</story>";
$parser->setHandlers(
Start => \&start_handler,
Char => \&char_handler,
End => \&end_handler);
$parser->parse($xmlStr);
print Dumper @pagesArray;
exit;
sub char_handler
{
my ($p, $data) = @_;
if($pageFlag ==1 && $authorFlag ==1 ){
$authorTxt .= $data;
}
if($pageFlag ==1 && $keywordFlag ==1 ){
$keywordTxt .= $data;
}
if($pageFlag ==1 && $headlineFlag ==1 ){
$headlineTxt .= $data;
}
if($pageFlag ==1 && $imageFlag ==1 ){
$imageTxt .= $data;
}
if($pageFlag ==1 && $descFlag ==1 ){
$descTxt .= $data;
}
}
sub start_handler
{
my ($p, $data) = @_;
if($data =~ /^(page)$/) {
%hash = ();
$pageFlag = 1;
}
if($data =~ /^author$/) {
$authorFlag = 1;
}
if($data =~ /^keywords$/) {
$keywordFlag = 1;
}
if($data =~ /^headline$/) {
$headlineFlag = 1;
}
if($data =~ /^image$/) {
$imageFlag = 1;
}
if($data =~ /^description$/) {
$descFlag = 1;
}
}
sub end_handler
{
my ($p, $data) = @_;
if($data =~ /^author$/) {
$hash{author} = $authorTxt;
$authorTxt = '';
print "Author -- $hash{author} \n";
$authorFlag = 0;
}
if($data =~ /^keywords$/) {
$hash{keywords} = $keywordTxt;
$keywordTxt = '';
print "Keyword -- $hash{keywords}\n";
$keywordFlag = 0;
}
if($data =~ /^headline$/) {
$hash{headline} = $headlineTxt;
$headlineTxt = '';
print "Head -- $hash{headline}\n";
$headlineFlag = 0;
}
if($data =~ /^image$/) {
$hash{image} = $imageTxt;
$imageTxt = '';
print "Image -- $hash{image}\n";
$imageFlag = 0;
}
if($data =~ /^description$/) {
$hash{description} = $descTxt;
$descTxt ='';
print "Desc -- $hash{description}\n";
$descFlag = 0;
}
if($data =~ /^page$/) {
push(@pagesArray,\%hash);
}
}