Using a combination of
Text::CSV and
Template Toolkit would be a flexible solution. If you have a complex format a templating system gives you the very fine control you need.
Use Text::CSV to convert your file to a structure like:
@rows = (
{
Col1 => 1,
Col2 => 'AB12345',
Col3 => '03.04.2016 15:43:14',
Col4 => '-76775.70',
Col5 => 'Toll road INC',
},
{
Col1 => '2',
Col2 => 'CD12345',
Col3 => '01.04.2016 16:39:15',
Col4 => '-76775.70',
Col5 => 'Toll road INC',
},
);
Then pass that structure to Template Toolkit
my $params = { rows => \@rows };
my $template = Template->new;
$template->process('template.xml', $params, 'output.xml');
Your template file 'template.xml' could look something like:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Import>
[% FOREACH row IN rows %]
<record>
<Col1>[% row.Col1 %]</Col1>
<Col2>[% row.Col2 %]</Col2>
[% IF row.Col3 %]
<Col3>[% row.Col3 %]</Col3>
[% END %]
<Col4>[% row.Col4 %]</Col4>
<Col5>[% row.Col5 %]</Col5>
</record>
[% END %]
</Import>
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.