Sahal
Sahal

Reputation: 113

How to fetch data from XML file in Perl?

I need to get the value of <Title> from XML file with Perl. I tried with Twig but still value is not fetching.

This is the XML file:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Specification SchemaVersion="4.3">
<Title><![CDATA[Txt to fetch]]></Title>

#more tags here

</Specification>

Here the sample Perl code which I tried:

my $openXml=XML::Twig->new();
$openXml->parsefile($filepath);
foreach my $root($openXml->root->children('Specification')){
$title=$root->first_child('Title')->att('string');
}

I need to take the value from that Title tag.

Upvotes: 0

Views: 88

Answers (1)

Bruce
Bruce

Reputation: 1542

This code snippet should get the title text from the xml file.

my $filepath = "xml_file.xml";

my $openXml = XML::Twig->new();
$openXml->parsefile($filepath);
my $title = $openXml->root->first_child_text('Title');
say $title; 

Upvotes: 2

Related Questions