http://www.perlmonks.org?node_id=1051260

perladdict has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks,
I am new to the XML, this is the first time i am trying to learn the parsing of XML by using XML::parser module. I have created a sample xml and trying to print the line numbers at with each element in the xml sample file in the perl script.
The script is compiling with no errors but it is not displaying the elements and its line numbers. Below is the code and the sample xml file,please provide your valuable suggestions what is wrong in the script for not printing the output.
use XML::Parser:Expat; $file = "..\\sample.xml"; my $parser = XML::Parser::Expat(Handlers => {Start => \&handle_start, End => \&handle_end,}); $parse->parsefile($file); my @element_stack; sub handle_start { my($expat,$element,%attrs) = @_; my $line = $expat->current_line; print "I can see $element element starting on line: $line\n"; push(@element_stack,{element=>$element,line=$line}); if(%attrs){ while(my($key,$value) = each(%attrs)) { print "\t$key=>$value\n"; } } } sub handle_end { my($expat,$element) = @_; my $element_record = pop(@element_stack); print "i can see the $element element started on line ",$$element_reco +rd{line}, "is closing now\n"; }
Below is the sample xml file
<?xml version="1.0"?> <spam-document version="3.5" timestamp="2013-08-28 15:23:15"> <customer> <first-name>william</first-name> <last-name>hong</last-name> <address> <street>311 Lords plaza</street> <city>boston</city> <zip>91001</zip> </address> </customer> <customer> <first-name>Walter</first-name> <last-name>fey</last-name> <address> <street>Eastern tower</street> <city>denver</city> <zip>91020</zip> </address> </customer> </spam-document>