0

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.

5
  • show your code as well. Commented Aug 29, 2019 at 9:06
  • @Bruce I added my code in the question Commented Aug 29, 2019 at 9:15
  • Always use strict; use warnings; See perlmaven.com/always-use-strict-and-use-warnings Commented Aug 29, 2019 at 9:38
  • @Bruce I added that and I forgot to add it in the code here . then also output is not fetching Commented Aug 29, 2019 at 9:55
  • You're using the att() method, which means you are looking for the attribute "string" in the element "Title", as in <Title string="test">Text here</Title> Commented Aug 29, 2019 at 10:02

1 Answer 1

2

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; 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.