Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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>

In reply to Re: Convert CSV to XML by tangent
in thread Convert CSV to XML by andreas1234567

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (9)
As of 2024-04-23 14:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found