如何获取未知的根标记解析功能

时间:2012-10-22 04:53:09

标签: perl xml-twig

我在进行xml转换,这里我将root标签定义为book,但是如何在这里定义unknow root标签,我需要常见的syntex,

use twig;

my $story_file = XML::Twig->new(
                                   twig_handlers =>{
                                            'book' => \&book,
                                            keep_atts_order => 1,
                                            },
                                            pretty_print => 'indented',
                                  );

$story_file->parsefile('unknown.xml');
$story_file->print($out);

我试过了,

use twig;

my $story_file = XML::Twig->new(
                                   twig_handlers =>{
                                            root => \&book,
                                            keep_atts_order => 1,
                                            },
                                            pretty_print => 'indented',
                                  );

$story_file->parsefile('unknown.xml');
$story_file->print($out);

它不起作用,如果任何comman syntex为root标签,则使用示例

1 个答案:

答案 0 :(得分:1)

twig_handlers的键是XPath,因此您可以使用/*

use XML::Twig;

sub root { print "root\n"; }

my $story_file = XML::Twig->new(
   twig_handlers =>{
      '/*' => \&root,
   },
   keep_atts_order => 1,
   pretty_print    => 'indented',
);

$story_file->parsefile('unknown.xml');
$story_file->print($out);

我安装了3.41。