My input file is
TBLA COLA A B TBLA COLB D E TBLB COLX M N TBLB COLD A B TBLC COLD
Recommend is to use XML::Simple instead of writing an selfmade XML Parser. You just need to set:
use XML::Simple; my $xml = XMLout($hashref, RootName => 'Data');