All pastes #1821704 Raw Edit

cvs2xml.pl

public text v1 · immutable
#1821704 ·published 2010-03-03 19:45 UTC
rendered paste body
#! /usr/bin/perl
use warnings;
use strict;
use Text::CSV;

# generate XML from CSV files
#


my $csv = Text::CSV->new(
    {
        binary => 1, # binary for utf8, latin1 etc
        sep_char => "\t"
    }
) or die "Cannot use CSV: ".Text::CSV->error_diag ();

$csv->eol("\r\n");

binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");

foreach (@ARGV) {
    open my $input, "<:encoding(utf8)", $_ or die "test.csv: $!";

    print "<csv filename=\"$_\">";
    while (my $colref = $csv->getline($input)) {
        print "<row>" .
        join("", map { "<cell>$_</cell>" } @$colref ) .
              "</row>\n";
    }
    print "</csv>\n";
}