如何使用XML :: Twig打印元素名称 [英] How to print element names using XML::Twig
本文介绍了如何使用XML :: Twig打印元素名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
谢谢,在这个论坛上,我能够成功编写一个perl脚本来编辑xml文件.但是我想在屏幕上打印一些东西.我想显示更改前后的xml内容.请帮帮我.
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.
输入XML
<config>
<match_name>Match_20111010</match_name>
<teamA>Alpha_2353523</teamA>
<teamB>Beta_23523523</teamB>
<result>Win</result>
</config>
代码
#!/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;
}
推荐答案
对STDOUT
使用print()
函数:
$twig->print(\*STDOUT);
或仅打印标签名称:
print STDOUT $tag->name;
这篇关于如何使用XML :: Twig打印元素名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文