use strict; use XML::Twig; my $org = <<'XML'; XML # parse the input string my $root = XML::Twig->new (); $root->parse ($org); # Insert a new element my $ins = XML::Twig::Elt->new ('LOCATION', {name => '1stFloor'}); $ins->paste ('last_child', $root->root ()); # Dump the result $root->print ();