in reply to
Re: how to update an xml file by read data from csv file
in thread how to update an xml file by read data from csv file
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.