in reply to Modifying a Sepcifig node

Sounds like a perfect job for XML::Twig.
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

    Hi All,

    The above code is perfectly working except one problem. I do not know why it is happening like this. My problem is &apos; is getting changed to '(its character). But I want &apos; in the output

    Please help in getting the &apos; in the output

    Regards

    Ganesh

      It doesn't really matter, but if you insist, try keep_encoding option.