use strict; use warnings; use XML::Writer; #use XML::Reader; use IO::File; open(my $fh,"./input.txt") or die "Cannot open input file."; my $output = new IO::File(">converted_to_xml.xml"); #file to write the converted content my $writer = new XML::Writer(OUTPUT => $output); #write into the opened file my @array=qw(name emp_no designation salary); $writer->xmlDecl("UTF-8"); #XML declaration $writer->startTag("data"); # The root tag while(my $input=<$fh>){ chomp($input); next if !$input; my %vals; @vals{@array} = split /:/, $input; $writer->startTag("row"); # enclosing tag for every data row for my $name (@array) { $writer->startTag($name); $writer->characters($vals{$name}); $writer->endTag(); } $writer->endTag(); # = endTag("row") } $writer->endTag(); # = endTag("data") $writer->end(); $output->close();