Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

Unable to print the line numbers at witch the element found in the xml doc

by perladdict (Chaplain)
on Aug 28, 2013 at 09:25 UTC ( #1051260=perlquestion: print w/replies, xml ) Need Help??
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>

Replies are listed 'Best First'.
Re: Unable to print the line numbers at witch the element found in the xml doc
by kcott (Chancellor) on Aug 28, 2013 at 09:46 UTC

    G'day perladdict,

    Look at the XML::Parser::Expat documentation. Read the first few lines of the SYNOPSIS. See how your code differs from the published documentation.

    Then keep reading to acquaint yourself with the details!

    -- Ken

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
    not surprising, you're not using strict or warnings, also the code doesn't compile, you have several typos and you're not copy/pasting well enough .... copy/paste from XML::Parser Tutorial

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1051260]
Approved by kcott
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (5)
As of 2018-06-25 10:16 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.