in reply to Convert a Tab-Delimeted File to XML
OK, so here is a complete answer, that saves each line in a separate file.
First a couple of remarks:
So here it is:
#!/usr/bin/perl my $file_nb="000"; # write labels my (@labels) = split /\t/, <DATA>; my @labels= map { sanitize_label( $_) } @labels; my $file= "data-$file_nb.xml"; open( LABELS, ">$file") or die "cannot open $file: $!"; print LABELS qq{<?xml version="1.0" encoding="ISO-8859-1"?>\n}, "<labels>", map( { "<col>" . $_ . "</col>"} @labels), "</labels>\n"; close LABELS; # write data while (<DATA>) { my %line; chomp; @line{@labels} = split /\t/; $file_nb++; my $file= "data-$file_nb.xml"; open( XML, ">$file") or die "cannot open $file: $!"; print XML qq{<?xml version="1.0" encoding="ISO-8859-1"?>\n}, qq{<data record_no="$file_nb">}, map( { "<$_>" . xml_escape( $line{$_}), "</$_>"} @labels +), "</data>\n"; close XML; } # dumb way to make label valid XML names: remove all non word characte +rs sub sanitize_label { my $label= shift; $label=~ s/[\W]//g; return $label; } # just escape the minimum: < and & sub xml_escape { my $text= shift; $text=~ s/&/&/g; $text=~ s/</</g; return $text; } __DATA__ First Last Fred Flintstone Barney Rubble & all Betty Rubble Wilma Flintstone
|
|---|