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>
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.