使用XML::Twig
,有没有办法获取节点的整个HTML?我不想要节点的文本,而是整个带标签的HTML和所有。
输入XML
<content> <p>blah blah <b> bla bla </b> </p>
<p> line 2 <i> test </i? </p>
</content>
代码
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
应该向我显示整个HTML。
答案 0 :(得分:5)
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;