Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
As I am new to Perl, I need to write a Perl script which converts csv to xml.

sample csv:

Name,Company,Email,Phone A,X,a.x@aol.com,1111 B,Y,b.y@aol.com,0 C,Z,c.z@aol.com,2222
Current script:
use strict; use warnings; use XML::Writer; use IO::File; use Data::Dumper; my $CSVfile="1.csv"; my $xmlFile="1.xml"; print "Input Csv file: $CSVfile and Output XML: $xmlFile\n"; my $output = IO::File->new(">$xmlFile"); my $writer = XML::Writer->new( OUTPUT => $output, DATA_MODE => 'true', UNSAFE => 1, DATA_INDENT => 6 ); $writer->xmlDecl("UTF-8"); my $rootTag="row"; my ($arrRef,$keys)= convertCsvToHash($CSVfile); for my $hash(@$arrRef){ writeXML($writer,$hash,$rootTag); } $writer->end(); $output->close(); sub writeXML{ my ($writer,$hash, $tagname, %attrs) = @_; $writer->startTag( $tagname, %attrs ); if (ref $hash eq "HASH" ){ for my $key(@$keys){ writeXML($writer,$hash->{$key},$key); } }else{ $writer->characters($hash); } $writer->endTag($tagname); } sub convertCsvToHash{ my $file = shift; open(FILE, "$file"); my @arr = <FILE>; close(FILE); my @hashArry; chomp($arr[0]); my @keys = split(',',$arr[0]); my @ke=my ($key1, $key2) = @keys[1,3]; for my $i(1..$#arr){ chomp($arr[$i]); my @splitRec = split(',',$arr[$i]); my $hash = {}; push (my @push, ($splitRec[1],$splitRec[3])); if ($push[1] ne 0) { for my $j(0..$#ke){ $hash->{$ke[$j]} = $push[$j]; } push @hashArry,$hash; } } return \@hashArry, \@ke; }
I get after running the above:
<?xml version="1.0" encoding="UTF-8"?> <row> <Company>X</Company> <Phone>1111</Phone> </row> <row> <Company>Z</Company> <Phone>2222</Phone> </row>
Expected ouput:
<?xml version="1.0"?> <!DOCTYPE reference PUBLIC "-/EN" "reference.dtd"> <reference id="desc"> <title>Sample XML</title> <refbody> <section id="section_1"> <p> </p> <table id="table_1"> <tgroup cols="4"><colspec colnum="1" colname="col1" colwidth +="1.00*"/><colspec colnum="2" colname="col2" colwidth="1.00*"/> <thead> <row> <entry colname="col1">Company</entry> <entry colname="col2">Phone</entry> </row> </thead> <tbody> <row> <entry colname="col1">X</entry> <entry colname="col2">1111</entry> </row> </tbody> <tbody> <row> <entry colname="col1">Z</entry> <entry colname="col2">222</entry> </row> </tbody> </tgroup> </table> </section> </refbody> </reference>
Any help would be greatly appreciated, thanks

In reply to csv to xml by vavz

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 studying the Monastery: (2)
As of 2024-04-26 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found