Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Thanks bitingduck (Pilgrim) for you input. As you suggested i used Text::CSV module to read csv file.

Main pl file :
#!/usr/bin/perl use warnings; use strict; use warnings; use Text::CSV; my $file1 = 'C:\TR_Vijay\Automation\serverinput.csv'; my $csv = Text::CSV->new(); my @columns; open (CSV, "<", $file1) or die $!; while (<CSV>) { next if ($. == 0); if ($csv->parse($_)) { @columns = $csv->fields(); print "$columns[0] \n"; print "$columns[1] \n"; print "$columns[2] \n"; print "$columns[3] \n"; } else { my $err = $csv->error_input; print "Failed to parse line: $err"; } open my $file, '>>', 'result.xml' or die "Can't open file: $!"; print $file <<EOF <?xml version="1.0" encoding="UTF-8"?> <!-- <!*************************************************************** +*> --> <!-- <!* + *> --> <!-- <!* PuTTY Configuration Manager save file - All right reserved. +*> --> <!-- <!* +*> --> <!-- <!*************************************************************** +*> --> <!-- <!The following lines can be modified at your own risks.> --> <configuration version=$columns[0] savepassword="True"> <root type="database" name=$columns[1] expanded="True"> <container type="folder" name=$columns[2] expanded="True"> <container type="folder" name=$columns[3] expanded="True"> <connection type="PuTTY" name=$columns[4]> <connection_info> <name>$columns[4]</name> <protocol>SSH</protocol> <host>$columns[7]</host> <port>$columns[8]</port> <session>Default Settings</session> <commandline>$columns[10]</commandline> <description /> </connection_info> <login> </login> <timeout> <connectiontimeout>1000</connectiontimeout> <logintimeout>750</logintimeout> <passwordtimeout>750</passwordtimeout> <commandtimeout>750</commandtimeout> </timeout> <command> </command> <options> <loginmacro>False</loginmacro> <postcommands>False</postcommands> <endlinechar>10</endlinechar> </options> </connection> </root> </configuration> EOF ; } close CSV;
I was able to achive almost expect for few things. Anyhelp would be much appreciated. Achieved so far : 1. able to read data from csv file 2. ouput the xml file with data read from csv file. Now Issues are : 1. Don't want to print first 7 lines of the xml format everytime. It should be printed only once at top of the file.
<?xml version="1.0" encoding="UTF-8"?> <!-- <!*************************************************************** +*> --> <!-- <!* + *> --> <!-- <!* PuTTY Configuration Manager save file - All right reserved. +*> --> <!-- <!* +*> --> <!-- <!*************************************************************** +*> --> <!-- <!The following lines can be modified at your own risks.> -->
2. Don't want to print last 2 line everytime. it just need to be printed only once at bottom of the file
</root> </configuration>
3. Not sure where i made mistake, but when ever i try to open the result.xml, it doesn't open. even program doesn't give any error thou.

In reply to Re^2: how to update an xml file by read data from csv file by Vijay81
in thread how to update an xml file by read data from csv file by Vijay81

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 imbibing at the Monastery: (5)
As of 2024-03-29 07:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found