sprint
の代わりにXML::Twigs メソッドを使用しますxml_string
。ドキュメントは次のように述べています。
xml_string @optional_options
Equivalent to $elt->sprint( 1), returns the string for the entire element, excluding the element's tags (but nested element tags are present)
そのsprint
関数を検索すると、次の結果が得られます。
スプリント
Return the text of the whole document associated with the twig. To be used only AFTER the parse.
したがって、次のことができます。
use strict;
use warnings;
use Data::Dumper;
use XML::Twig;
my @array=();
my $twig = XML::Twig->new(
twig_handlers => {
'data'=> sub {push @array, $_->sprint;}
});
$twig->parse(qq~
<xml>
<data id="foo">
<deep>
<deeper>the deepest</deeper>
</deep>
</data>
</xml>
~);
print Dumper \@array;
どちらが印刷されますか:
$VAR1 = [
'<data id="foo"><deep><deeper>the deepest</deeper></deep></data>'
];