Bijan
Bijan

Reputation: 8670

Perl: Extract element from XML::Twig

How would I extract a single element from an XML document using XML::Twig?

XML Code:

<report>
    <reportheader>
        <month>February 2015</month>
    </reportheader>
</report>

What I've tried

XML::Twig->new(
    twig_handlers => {
        '/report/reportheader/month' => sub {
            printf qq|%s\n|, $_;
        },
    },  
)->parsefile($ARGV[0]);

But this just outputs XML::Twig::Elt=HASH(0x343af70). What am I missing?

Upvotes: 1

Views: 358

Answers (2)

Borodin
Borodin

Reputation: 126762

Although it is tempting to use the callback system of XML::Twig, it is often more simple to simply parse the entire XML data into an 'XML::Twig' data structure and access the contents of that using findnodes etc.

Here's an example that prints the text content of all elements that match the XPath expression /report/reportheader/month.

use strict;
use warnings;
use 5.010;     # For `say`

use XML::Twig;

my $twig = XML::Twig->new;
$twig->parsefile(shift @ARGV);

for my $report_month ( $twig->findnodes('/report/reportheader/month') ) {
  say $report_month->trimmed_text;
}

output

February 2015

Upvotes: 2

Birei
Birei

Reputation: 36282

It's because in the anonymous subrutine, $_ is the element object. If you want to print the text of the element use:

printf qq|%s\n|, $_->text_only;

That yields:

February 2015

Or if you want to print the whole element, use instead:

printf qq|%s\n|, $_->outer_xml;

That yields:

<month>February 2015</month>

And you can look at the docs, they are pretty clear.

Upvotes: 3

Related Questions