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):
Now, isn't that a lot simpler? That's the whole point of using an XML parser - to make things simpler.#!/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' ); }
In reply to Re: Bug in XML::Parser
by graff
in thread Bug in XML::Parser
by manunamu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |