#!/usr/bin/perl
if ($#ARGV < 0)
{
print "Usage : $0 <FlatFile Name>\n";
exit;
}
my $in_txt=shift @ARGV;
my $out_xml=$in_txt;
$out_xml=~s/\.txt/\.xml/g;
open(INFO,"<$in_txt");
open(XML,">$out_xml");
print XML "<\?xml version=\"1.0\" encoding=\"UTF-16\"\?>\n";
print XML "<employee>\n";
while(<INFO>)
{
if (/\A(.*?),(\d*+),(.*?),(\d*+),(\d*-\d*-\d+)/is)
{
print XML "<Employee_Detail>\n\t<Name>$1<\/Name>\n\t<Id>$2<\/Id>\n\t<State>$3<\/State>\n\t<Extension>$4<\/Extension>\n\t<Mobile>$5<\/Mobile>\n<\/Employee_Detail>\n";
}
}
print XML "<\/employee>";
print "\"$in_txt\" got successfully converted to \"$out_xml\"";
employee.txt - The Flat File
------------------------------------------------------------------
Rabindra Nayak,23371,New Jercy,2370,212-555-6868
Mark Perino,23750,New York,2459,212-555-7979
Adam Gill,18654,DC,3897,212-555-5757
Tim Anderson,23456,Virginia,6543,212-555-3030
Mike Gotham,22500,Phoenix,7856,212-555-8989
if ($#ARGV < 0)
{
print "Usage : $0 <FlatFile Name>\n";
exit;
}
my $in_txt=shift @ARGV;
my $out_xml=$in_txt;
$out_xml=~s/\.txt/\.xml/g;
open(INFO,"<$in_txt");
open(XML,">$out_xml");
print XML "<\?xml version=\"1.0\" encoding=\"UTF-16\"\?>\n";
print XML "<employee>\n";
while(<INFO>)
{
if (/\A(.*?),(\d*+),(.*?),(\d*+),(\d*-\d*-\d+)/is)
{
print XML "<Employee_Detail>\n\t<Name>$1<\/Name>\n\t<Id>$2<\/Id>\n\t<State>$3<\/State>\n\t<Extension>$4<\/Extension>\n\t<Mobile>$5<\/Mobile>\n<\/Employee_Detail>\n";
}
}
print XML "<\/employee>";
print "\"$in_txt\" got successfully converted to \"$out_xml\"";
employee.txt - The Flat File
------------------------------------------------------------------
Rabindra Nayak,23371,New Jercy,2370,212-555-6868
Mark Perino,23750,New York,2459,212-555-7979
Adam Gill,18654,DC,3897,212-555-5757
Tim Anderson,23456,Virginia,6543,212-555-3030
Mike Gotham,22500,Phoenix,7856,212-555-8989