Bootstrap FreeKB - Perl (Scripting) - XML::Twig print values (text)
Perl (Scripting) - XML::Twig print values (text)

Updated:   |  Perl (Scripting) articles

Let's say foo.xml contains the following.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>


Here is how you would parse foo.xml and print the XML tags using XML::Twig.

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

my $twig = new XML::Twig;


The following should be returned.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>


children can be used to only print the <name> tags.

foreach my $name ($twig->root->children('name')) {


Which should return the following.

  <name>Bugs Bunny</name>


text can be used to only print the value in a tag.

foreach my $twigname ($twig->root->children('name')) {
  my $name = $twigname->text;
  print $name;


Which should return the following.

Bugs Bunny

Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee


Add a Comment

Please enter 3c1306 in the box below so that we can be sure you are a human.