2
#!/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>
4

1 回答 1

2

您正在寻找的方法是inner_xml.

于 2012-11-02T08:25:01.057 回答