Search code examples
xmlperlxml-twig

Outputting XML::Twig object contents


As an example, I have a Perl XML::Twig object $xmlDef, which contains the following:

<ROOT>
  <CHILD>
  </CHILD>
</ROOT>

It's generated with the following code:

    my $parser = XML::Twig->new(
            twig_handlers => {
                ROOT => sub { $xmlDef = $_ }
            },
            pretty_print  => 'indented'
    );
    $parser->parse($xmlStr);

When I view $xmlDef using $xmlDef->print, its contents get properly output to console. When I do print $xmlDef->text, nothing gets output. What am I doing wrong by using ->text?


Solution

  • The text method returns all the text content of the given element. Try adding some text to your XML:

    <ROOT>
      <CHILD>
        This will be printed.
      </CHILD>
    </ROOT>