$ cat j.pl #!/usr/bin/perl use strict; use XML::Twig; my $twig = XML::Twig->new( pretty_print => 'indented' )->parse('<users/>')->root; my @users = ( { firstname => 'Jay', lastname => 'Hannah' }, { firstname => 'Quentus', lastname => 'Rex' }, ); foreach (@users) { my $elt = XML::Twig::Elt->new( user => { firstname => $_->{firstname}, lastname => $_->{lastname} } ); $elt->paste( last_child => $twig ); } print $twig->sprint; $ perl j.pl <users> <user firstname="Jay" lastname="Hannah"/> <user firstname="Quentus" lastname="Rex"/> </users>