Reputation: 3262
How can I return the entire xml tag using xml::twig and save it to array:
for example :
my @array=();
my $twig = XML::Twig->new(
twig_handlers => {
'data'=> sub {push @array, $_->xml_string;}
});
This code return all the nested tags but without the tag itself and its properties is there an option to return the entire tag using xml::twig and save it to varaible ?
Upvotes: 0
Views: 615
Reputation: 54333
Use XML::Twigs method sprint
instead of xml_string
. The docs say that:
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)
A search for that sprint
function yields:
sprint
Return the text of the whole document associated with the twig. To be used only AFTER the parse.
Thus, you can do the following:
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;
Which prints:
$VAR1 = [
'<data id="foo"><deep><deeper>the deepest</deeper></deep></data>'
];
Upvotes: 5