如何使用XML :: Twig获取其他标签的标签内容?

时间:2012-11-02 07:27:29

标签: xml perl xml-twig

#!/usr/bin/perl

use strict;
use warnings FATAL => 'all';
use 5.010;

use XML::Twig;

sub get_xml_from_data {
    my @lines = <DATA>;
    my $xml = join "", @lines;

    return $xml;
}

sub main {
    my $xml = get_xml_from_data();

    my $xt = XML::Twig->new();
    $xt->parse($xml);
    my $root= $xt->root;

    # output 1
    say $root->first_child('element')->sprint;
    # It will print:
    #
    #   <element>
    #           Some content with <b>some</b> <i><b>other</b></i> tags.
    #        </element>

    # output 2
    say $root->first_child('element')->text;
    # It will print:
    #
    #        Some content with some other tags.


    # But I need the output:
    #
    #           Some content with <b>some</b> <i><b>other</b></i> tags.

}

main();

__DATA__
<root>
    <element>
        Some content with <b>some</b> <i><b>other</b></i> tags.
    </element>
</root>

1 个答案:

答案 0 :(得分:2)

您要查找的方法是inner_xml