How to print element names using XML::Twig

╄→гoц情女王★ 提交于 2019-12-11 02:49:23

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!