1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
| #!/usr/bin/perl
use Carp;
use strict;
use warnings;
use XML::Twig;
my $twig = XML::Twig->new(
pretty_print => 'indented',
Twig_handlers => {
'domiciliation/nomBanque' => \&nombanque,
'virement/automatique' => \&virement,
},
);
$twig->parsefile( 'TOTO.xml');
open my $fh, '>', 'TOTOmodifie.xml';
$twig->print($fh);
close $fh;
sub nombanque {
my ($twig, $twig_NomBanque) = @_;
print "Je suis dans la balise ",$twig_NomBanque->name,"\n";
my $NomBanque = $twig_NomBanque->text;
print "==> ",$NomBanque,"\n";
return;
}
sub virement {
my ($twig, $twig_automatique) = @_;
print "Je suis dans la balise ",$twig_automatique->name,"\n";
# Cherchons le nom de la Banque
# Cherchons le frere du parent de virement de type contenant nomBanque
my $twig_parent = $twig_automatique->parent;
# Cherchons le frere de virement (domiciliation)
my $twig_NomBanque;
if ( defined $twig_parent ) {
if ( my $twig_domiciliation = $twig_parent->prev_sibling('domiciliation') ) {
# Cherhons la banque
if ( my $banque = $twig_domiciliation->field('nomBanque') ) {
print "Ma banque : $banque\n";
print "Automatique : ", $twig_automatique->text, "\n";
}
}
}
return;
} |