richardtk_1
richardtk_1

Reputation: 761

How to extract CDATA content with TWIG using Perl

I am new to Perl, so I have little experience using it, and I have to extract the content inside CDATA using TWIG and Perl from the following xml file:

    <?xml version='1.0'  encoding="utf-8"?>
    <text>
    <![CDATA[
    1. Democracia ukata Estado de Derecho sutinchata kamachinaka ch’amanchañataki

    ...
    ]]>
    </text>

Sorry if this question is repeated somewhere. Thanks in advance.

Upvotes: 2

Views: 899

Answers (1)

Birei
Birei

Reputation: 36282

Use twig_handlers to do filtering using #CDATA as xpath expression.

Content of script.pl:

#!/usr/bin/env perl

use warnings;
use strict;
use XML::Twig;

my $twig = XML::Twig->new( 
    twig_handlers => {
        '#CDATA' => sub { print $_->text },
    },  
)->parsefile( shift );

Run it like:

perl script.pl xmlfile

That yields:

1. Democracia ukata Estado de Derecho sutinchata kamachinaka ch’amanchañataki

...

Upvotes: 6

Related Questions