Just another WordPress weblog
31 Dec
In my quest to convert a CSV file to XML I’ve found the best advice seems to be to use Perl.
Do I have Perl installed on my linux server?
http://www.cyberciti.biz/faq/how-do-i-find-out-what-perl-modules-already-installed-on-my-system/
Enter the following command in shell -
$ instmodsh
To find the version of perl -
http://www.cyberciti.biz/faq/how-can-i-find-out-perl-version/
$ perl -v
Here’s my findings:
I’ve been following a tutorial in a book called XML and Perl by Mark Riehl and Ilya Sterin, which is a very good read for a XML and Perl newbie like me.
Anyway, I’m using Linux Debian with Perl installed but no other Perl modules. Here’s what I did:
Installing Perl modules on linux debian
http://www.cyberciti.biz/faq/how-do-i-install-a-perl-module/
perl -MCPAN -e shell
This should display this:
CPAN is the world-wide archive of perl resources. It consists of about
100 sites that all replicate the same contents all around the globe.
Many countries have at least one CPAN site already. The resources
found on CPAN are easily accessible with the CPAN.pm module. If you
want to use CPAN.pm, you have to configure it properly.
If you do not want to enter a dialog now, you can answer ‘no’ to this
question and I’ll try to autoconfigure. (Note: you can revisit this
dialog anytime later by typing ‘o conf init’ at the cpan prompt.)
Are you ready for manual configuration? [yes]
Note: This took a long time and lots of files were installed
To install Perl modules:
Here is how you would install XML::SAX using CPAN.pm in the shell mode:
>perl −MCPAN −e shell
CPAN>install XML::SAX
To convert CSV and Excel files to XML you will need to install the modules:
XML::SAXDriver::CSV
XML::SAXDriver::Excel
Issues:
Text::CSV_XS – This module is required
Unfortunately there was a problem installing the required SAX modules so I needed a new approach. I did manage to install some modules using this method though.
I then found this post http://www.xml.com/pub/a/2001/06/13/perlxml.html
The initial code had a spelling mistake $xsl needs to be $xls
I created a new directory and then created a new Perl file, test.pl
use XML::Excel;
my $file = ‘inventory.xls’;
my @columns = (’key’, ‘lname’, ’size’, ‘form’);
my $xls = XML::Excel->new({column_headings => \@columns});print $file;
$xls->parse_doc($file);
$xls->declare_xml({version => ‘1.0′,
standalone => ‘yes’});
$xls->print_xml(’test.xml’,
{file_tag => ‘inventory’,
parent_tag => ‘entry’}
);
Then I uploaded the Excel file inventory.xls to the same directory. Finally I executed the Perl script:
# perl test.pl
And it output the XML file test.xml as required.
Leave a reply