tags:

views:

28

answers:

1

Using XML::Twig, is there a way to get the entire HTML of a node? I do not want the text of the node, but the entire HTML with tags and all.

input XML

<content> <p>blah blah <b> bla bla </b> </p>
<p> line 2 <i> test </i? </p>
</content>

Code

my $twig = new XML::Twig(
    TwigRoots    => {'content' => 1},
    TwigHandlers => $twig_handlers
);
my $twig_handlers = {'count/p' => \&count_ps};

sub count_ps {
    my ($twig, $test) = @_;
    $Data .= $test->text();
}

$data should show me the entire HTML.

+1  A: 

Use the xml_string method.

my $data;
XML::Twig->new(
    twig_handlers => {
        content => sub {
            my ($twig, $elt) = @_;
            $data = $elt->xml_string;
            $twig->purge;
        }
    }
)->parse('    <content> <p>blah blah <b> bla bla </b> </p>
    <p> line 2 <i> test </i> </p>
    </content>
')->purge;
daxim