Subject: | flush() repeats the last input line |
Hi Michel,
First of all thanks for XML::Twig. I use it to process large XML files
with database records. I have a problem with flush which I think is a bug.
I use flush in my handler and a final flush at the end as described in
the documentation. Sometimes I get the correct result and sometimes I
get the last tag twice. It depends on how often flush is called. A
simple example shows that calling flush more than once repeats the last
input line.
My handler either converts a record + flush or ignores a record + no
flush. I cannot leave out the flush in the script, because than I
sometimes miss the last tag. Is this a bug or a feature ;-) ?
Thanxs,
Harco
Input:
<?xml version="1.0" encoding="UTF-8" ?>
<records xmlns:dbi='http://axkit.org/NS/xml-generator-dbi'>
<record>
<drivergroup>DCE ONG</drivergroup>
<usd_id>103995</usd_id>
<status>0</status>
<usd_modtime>1139308221</usd_modtime>
</record>
<record>
<drivergroup>Network WAN Planning</drivergroup>
<usd_id>832605</usd_id>
<status>0</status>
<usd_modtime>1139308424</usd_modtime>
</record>
</records>
script:
#!/usr/bin/perl -w
use XML::Twig;
my ($file, $elt)= @ARGV;
my $t= XML::Twig->new( twig_handlers =>
{ $elt => sub {$_[0]->flush; print "\n[flushed here]\n";} });
$t->parsefile( $file, ErrorContext => 2);
$t->flush; # in my script, depending on the flush in the handler
needed or not needed
$t->flush; # not in my script, but shows that flushing twice is harmfull
$t->flush; # not in my script
print "\n";
output:
?xml version="1.0" encoding="UTF-8"?>
<records
xmlns:dbi="http://axkit.org/NS/xml-generator-dbi"><record><drivergroup>DCE
ONG</drivergroup><usd_id>103995</usd_id><status>0</status><usd_modtime>1139308221</usd_modtime></record>
[flushed here]
<record><drivergroup>Network WAN
Planning</drivergroup><usd_id>832605</usd_id><status>0</status><usd_modtime>1139308424</usd_modtime></record>
[flushed here]
</records></records></records>