in reply to A Small XML::Parser issue
The documentation for the Char handler says this:
A single non-markup sequence of characters may generate multiple calls to this handler
So you need to accumulate the data and only print it in the end handler. Something like this perhaps:
use strict; use XML::Parser; my $parser = new XML::Parser(ErrorContext => 2); my $xmlStr = "<data> <Company_Name>XYZ CMPNY</Company_Name> <ArgId>775340</ArgId> <First_Name>Carol & Jerry</First_Name> </data>"; my $writeDataFlag = 0; $parser->setHandlers( Start => \&start_handler, Char => \&char_handler, End => \&end_handler); $parser->parse($xmlStr); my $text; sub char_handler { my ($p, $data) = @_; if ($writeDataFlag) { $text .= $data; } } sub start_handler { my ($p, $data) = @_; if($data =~ /^(Company_Name|First_Name)$/) { $writeDataFlag = 1; } } sub end_handler { my ($p, $data) = @_; if ($writeDataFlag) { print "Data - [$text] \n"; $text = ''; $writeDataFlag = 0; } }
"The first rule of Perl club is you do not talk about Perl club." -- Chip Salzenberg
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: A Small XML::Parser issue
by siva kumar (Pilgrim) on Feb 16, 2007 at 12:47 UTC |