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); } }

In reply to XML Parser issue by siva kumar

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.