【问题标题】:How can I get tag content with other tags with XML::Twig?如何使用 XML::Twig 获取带有其他标签的标签内容?
【发布时间】:2012-11-02 07:27:29
【问题描述】:
#!/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>

【问题讨论】:

    标签: xml perl xml-twig


    【解决方案1】:

    您要查找的方法是inner_xml

    【讨论】:

    • 谢谢!这正是我所需要的! =)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多