perladdict has asked for the wisdom of the Perl Monks concerning the following question:
Below is the sample xml fileuse 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"; }
<?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>
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Unable to print the line numbers at witch the element found in the xml doc
by kcott (Archbishop) on Aug 28, 2013 at 09:46 UTC | |
|
Re: Unable to print the line numbers at witch the element found in the xml doc
by Anonymous Monk on Aug 28, 2013 at 09:37 UTC |