如何使用XML :: Twig打印元素名称

时间:2013-11-18 15:42:56

标签: xml perl xml-twig

谢谢,在这个论坛上,我能够成功编写一个perl脚本来编辑xml文件。但我想在屏幕上打印一些东西。我想在更改之前和之后显示xml内容。请帮帮我。

输入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 个答案:

答案 0 :(得分:0)

print()功能用于STDOUT

$twig->print(\*STDOUT);

或者只打印标签名称:

print STDOUT $tag->name;