#!/usr/bin/perl
use strict;
use warnings;
use XML::Twig;
my $twig;
my $root = "";
my $element;
my $firstelem;
my $childcnt;
$twig = XML::Twig->new(
output_encoding => 'utf8',
pretty_print => 'record');
# $root is a string containing the starting tag
$twig->parse($root);
$root = $twig->root;
# $root is now the root twig element, and we can modify it
$root->set_gi('nodetag_root');
# We can add children to it
foreach $childcnt (0 .. 10)
{
$element = XML::Twig::Elt->new('childtag' => 'child text');
$element->set_att('index',$childcnt);
$element->paste('last_child',$root);
}
# We can modify an arbitrary child
$element = $root->first_child('childtag[@index="5"]');
$element->set_text('Number Five, alive!');
# And we can print it, to a filehandle if necessary
$twig->print;
####
child text
child text
child text
child text
child text
Number Five, alive!
child text
child text
child text
child text
child text