use strict; use warnings; use XML::Simple; my $file = 'SINGLE.000'; open(AFILE, "<$file"); my @file = ; close AFILE; my (@array, @tags, @newfile, @listarray, @datatags, @dataarray, @defectdata); my ($currenttag, $line, $string, $currentline, $listindex, $bracket); my $index = 0; #counts line number in @file while ($index <= $#file){ $currentline = $file[$index]; $currentline =~ tr/\"/ /; #remove the quotes $currentline =~ s/\s*//; #removed the leading whitespace... if ($currentline =~ /Record/){ @array = split(/(\s+)/, $currentline); if(!$array[4]){$array[4] = 'none'}; $currentline = "\<$array[2] value\=\"$array[4]\"\>\n"; #write xml push(@tags, $array[2]); #push the RECORD type into the tags array for later closing push (@newfile, $currentline); $index++; } elsif ($currentline =~ /^\}/){ #if we hit a back bracket, end the data item and by popping off the last value in the hash $currenttag = pop(@tags); $currentline = "\<\/$currenttag\>\n"; #write xml push (@newfile, $currentline); $index++; } elsif ($currentline =~ /Field/){ $currentline =~ s/\{(.*)\}//; #pull data in between brackets. $line = $1; $line =~ s/\s//g; #clear out whitespace @array = split(/\s+/, $currentline); my $string = "\<$array[1]\>$line\<\/$array[1]\>\n"; #write xml push(@newfile, $string); $index++ } elsif($currentline =~ /^List/){ @array = split(/(\s+)/, $currentline); $currentline = "\<$array[2]\>\n"; push(@tags, $array[2]); push (@newfile, $currentline); $index++; $bracket = 1; @listarray = (); while ($bracket){ #read all dat in between the brackets and break into a list for hash keys chomp($file[$index]); push(@listarray, $file[$index]); if ($file[$index] =~ /\}/){$bracket = 0} $index++; } $string = join('', @listarray); $string =~ s/ImageList|float|int32|Columns|string|\s+|\{|\}|\d//g; #clean up list @datatags = split(/,/, $string); } elsif($currentline =~ /^Data/){ # this whole thing breaks up the long string of numbers into their approp. hash key value @array = split(/(\s+)/, $currentline); $currentline = "\\n"; push(@tags, 'DATA'); push (@newfile, $currentline); $index++; @dataarray = (); until ($file[$index] =~ /\}$/){ chomp($file[$index]); push(@dataarray, $file[$index]); $index++; } $string = join('', @dataarray); $string =~ s/\{|\}$|\"//g; #clean up list @dataarray = split(/\;/, $string); foreach (@dataarray){ $_ =~ s/\s+/,/g; #remove all the excess white space. }; foreach (@dataarray){ #each line in the data array needs to be split @defectdata = split(/\,/, $_); # here we split by comma shift (@defectdata); #first item is a space $currentline = "\\n"; #create defectid key push (@newfile, $currentline); $listindex = 0; foreach(@datatags){ unless($defectdata[$listindex]){$defectdata[$listindex] = 0}; $currentline = "\<$_\>$defectdata[$listindex]\<\/$_\>\n"; $listindex++; push(@newfile, $currentline); } $currentline = "\<\/i"."$defectdata[0]\>\n"; push (@newfile, $currentline); } } else{$index++}; } unshift(@newfile, "\<\?xml version\=\"1\.0\"\?\>\n"); #add the xml header info my $newfile = join('', @newfile); my $data = XMLin($newfile); my %data = %{$data}; open(BFILE, ">test.xml"); print BFILE @newfile; close BFILE;