in reply to Modifying a Sepcifig node
use strict; use warnings; use XML::Twig qw( ); my $fqn_dat = 'mlr.dat'; my $fqn_in = 'mlr_648.xml'; my $fqn_out = 'fixed_mlr_648.xml'; my %translations; { open(my $fh, '<', $fqn_dat) or die("Can't open \"$fqn_dat\": $!\n"); while (<$fh>) { /^(.*?)\t(.*)/ or next; $translations{$1} = $2; } } open(my $fh_in, '<', $fqn_in) or die("Can't open input file \"$fqn_in\": $!\n"); open(my $fh_out, '>', $fqn_out) or die("Can't create output file \"$fqn_out\": $!\n"); my $twig = XML::Twig->new( twig_handlers => { 'reference/title[@type="journal"]' => sub { my ($twig, $elt) = @_; my $text = $elt->text(); if ( exists( $translations{$text} ) ) { $elt->set_text( $translations{$text} ); } else { warn("No translation found for \"$text\"\n"); } }, 'reference' => sub { my ($twig, $elt) = @_; # Keep at most one <reference> in memory. $twig->flush($fh_out); }, } ); $twig->parse($fh_in); $twig->flush($fh_out);
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Modifying a Sepcifig node
by ngbabu (Novice) on Oct 15, 2008 at 12:44 UTC | |
by Anonymous Monk on Oct 15, 2008 at 12:51 UTC |