问题
Thanks, to this forum i was able to successfully write a perl script to edit xml file. But i would like to print something to the screen. I would like to display the xml contents before and after the change. Please help me out.
Input XML
<config>
<match_name>Match_20111010</match_name>
<teamA>Alpha_2353523</teamA>
<teamB>Beta_23523523</teamB>
<result>Win</result>
</config>
CODE
#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my $xml = 'config.xml';
my $twig = XML::Twig->new (
twig_roots => {match => \&edittag,
teamA => \&edittag,
teamB => \&edittag,
},
twig_print_outside_roots => 1,
);
$twig->parsefile_inplace($xml);
sub edittag {
my ($twig, $tag) = @_;
my $text = $tag->text ();
$text =~ s/\d+/REPLACED/;
$tag->set_text ($text);
$twig->flush;
}
回答1:
Use print()
function to STDOUT
:
$twig->print(\*STDOUT);
Or to print only the tag name:
print STDOUT $tag->name;
来源:https://stackoverflow.com/questions/20051790/how-to-print-element-names-using-xmltwig