As pointed out previously, your xml input file is bad - there's either an "extra" <CallDetailByService> open tag at line 208, or else you're missing a second </CallDetailByService> close tag at line 258. One way or the other, it's an easy thing to fix.

Apart from that, your main "foreach" loop indicates that you don't have a proper understanding yet of how to use XML::Parser. You should not be reading an xml file one line at a time and passing certain lines to the parser. That is absolutely the wrong way.

Use the parser to read (and parse) the entire file, and use the various handler subroutines to do what needs to be done as you encounter the elements of interest in the data. For example, if you want to print the contents of <Amount> elements to STDOUT, you could do something like this (after you fix your xml file):

#!/usr/bin/perl use strict; use warnings; use XML::Parser; my $current_element = my $current_amount = ""; my $p = XML::Parser->new( Handlers => { Start => \&handle_start, Char => \&handle_text, End => \&handle_end } ); $p->parsefile( "org1.xml" ); sub handle_start { my ( $xp, $element, %attr ) = @_; $current_element = $element; # keep track of where we are } sub handle_end { my ( $xp, $element ) = @_; if ( $element eq 'Amount' ) { # did we just close an "Amount" t +ag? print "$current_amount\n"; $current_amount = ""; } $current_element = ""; } sub handle_text { my ( $xp, $string ) = @_; # do stuff here depending on where we are now: $current_amount .= $string if ( $current_element eq 'Amount' ); }
Now, isn't that a lot simpler? That's the whole point of using an XML parser - to make things simpler.

In reply to Re: Bug in XML::Parser by graff
in thread Bug in XML::Parser by manunamu

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.