Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^3: how to update an xml file by read data from csv file

by bitingduck (Friar)
on Nov 30, 2012 at 10:00 UTC ( #1006444=note: print w/ replies, xml ) Need Help??


in reply to Re^2: 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

Here's a solution that uses XML::TreeBuilder (which uses XML::Element methods as well). It seemed easier than trying to build it up from print statements and regex replacements.

One maybe major caveat: your example input file isn't valid XML-- the two "container" tags don't get closed. I took the liberty of assuming (maybe incorrectly) that they should get closed and that you want valid XML.

Here's the code:
#!/usr/bin/perl use warnings; use strict; use Text::CSV; use XML::TreeBuilder; use Data::Dumper; my $xmlbase='original.xml'; my $paramsfile = 'params.csv'; # the list of tags filled from the csv file my @taglist=qw(name protocol host commandline); # this is the part that repeats, so we break it out separately # the tags that get filled from the CSV file are left empty my $connection= <<END <connection type="PuTTY" name="WSSS"> <connection_info> <name></name> <protocol></protocol> <host></host> <port>22</port> <session>Default Settings</session> <commandline></commandline> <description /> </connection_info> <login> <login /> <password /> <prompt /> </login> <timeout> <connectiontimeout>1000</connectiontimeout> <logintimeout>750</logintimeout> <passwordtimeout>750</passwordtimeout> <commandtimeout>750</commandtimeout> </timeout> <command> <command1 /> <command2 /> <command3 /> <command4 /> <command5 /> </command> <options> <loginmacro>False</loginmacro> <postcommands>False</postcommands> <endlinechar>10</endlinechar> </options> </connection> END ; #this is the wrapper my $config= <<END <?xml version="1.0" encoding="UTF-8"?> <!-- The following lines can be modified at your own risks. --> <configuration version="0.2.1.4" savepassword="True"> <root type="database" name="WSSS" expanded="True"> <container type="folder" name="CM" expanded="True"> <container type="folder" name="1-AD/M" expanded="True"> </container> </container> </root> </configuration> END ; #start an xml structure with the outer wrapper my $tree=XML::TreeBuilder->new(); $tree->parse($config); # find the insert location my $container=$tree->look_down("_tag"=>"container", "name"=>"1-AD/M"); + my $csv=Text::CSV->new(); open my $fh, "<:encoding(utf8)", $paramsfile or die "$paramsfile: $!"; # loop through the lines of the CSV and copy the connection structure +into # the wrapper with the updated values while(my $row=$csv->getline($fh)){ my $contree=XML::TreeBuilder->new(); #make an xml structure of th +e connection $contree->parse($connection); $container->push_content($contree); #insert it into the wrapper $contree->attr('name',$row->[0]); #set the name attribute $container->push_content("\n "); #make it a little prettier my $index=0; for my $tagname(@taglist){ #loop through the columns my $tag=$contree->look_down('_tag'=> $tagname); $tag->push_content($row->[$index++]); } } print '<?xml version="1.0" encoding="UTF-8"?>'."\n"; print $tree->as_XML();
And here's the output:
$ perl puttyconfig.pl <?xml version="1.0" encoding="UTF-8"?> <configuration savepassword="True" version="0.2.1.4"> <root expanded="True" name="WSSS" type="database"> <container expanded="True" name="CM" type="folder"> <container expanded="True" name="1-AD/M" type="folder"> <connection name="WSSSS" type="PuTTY"> <connection_info> <name>WSSSS</name> <protocol>SSH</protocol> <host>103.243.543.233</host> <port>22</port> <session>Default Settings</session> <commandline>erted</commandline> <description></description> </connection_info> <login> <login></login> <password></password> <prompt></prompt> </login> <timeout> <connectiontimeout>1000</connectiontimeout> <logintimeout>750</logintimeout> <passwordtimeout>750</passwordtimeout> <commandtimeout>750</commandtimeout> </timeout> <command> <command1></command1> <command2></command2> <command3></command3> <command4></command4> <command5></command5> </command> <options> <loginmacro>False</loginmacro> <postcommands>False</postcommands> <endlinechar>10</endlinechar> </options> </connection> <connection name="AWSA" type="PuTTY"> <connection_info> <name>AWSA</name> <protocol>SSH</protocol> <host>303.233.542.622</host> <port>22</port> <session>Default Settings</session> <commandline>rrertd</commandline> <description></description> </connection_info> <login> <login></login> <password></password> <prompt></prompt> </login> <timeout> <connectiontimeout>1000</connectiontimeout> <logintimeout>750</logintimeout> <passwordtimeout>750</passwordtimeout> <commandtimeout>750</commandtimeout> </timeout> <command> <command1></command1> <command2></command2> <command3></command3> <command4></command4> <command5></command5> </command> <options> <loginmacro>False</loginmacro> <postcommands>False</postcommands> <endlinechar>10</endlinechar> </options> </connection> </container> </container> </root> </configuration>


Comment on Re^3: how to update an xml file by read data from csv file
Select or Download Code
Re^4: how to update an xml file by read data from csv file
by Vijay81 (Acolyte) on Dec 01, 2012 at 23:23 UTC

    Thanks bitingduck (Pilgrim). Your suggested solution is very clear and neat way of producing xml format file. But only problem is that i don't want to display last 2 closing container tags. I did try removing that from wrapper but it didn't work. Is ther any way we can't avoid writing the last 2 container tags please..

    Error we get if we remove the closing tag :
    mismatched tag at line 7, column 4, byte 333 at C:/Perl64/lib/XML/Pars +er.pm line 187.
    Main program :
    #!/usr/bin/perl use warnings; use strict; use Text::CSV; use XML::TreeBuilder; use Data::Dumper; my $xmlbase='original.xml'; my $paramsfile = 'params.csv'; # the list of tags filled from the csv file my @taglist=qw(name protocol host commandline); # this is the part that repeats, so we break it out separately # the tags that get filled from the CSV file are left empty my $connection= <<END <connection type="PuTTY" name="WSSS"> <connection_info> <name></name> <protocol></protocol> <host></host> <port>22</port> <session>Default Settings</session> <commandline></commandline> <description /> </connection_info> <login> <login /> <password /> <prompt /> </login> <timeout> <connectiontimeout>1000</connectiontimeout> <logintimeout>750</logintimeout> <passwordtimeout>750</passwordtimeout> <commandtimeout>750</commandtimeout> </timeout> <command> <command1 /> <command2 /> <command3 /> <command4 /> <command5 /> </command> <options> <loginmacro>False</loginmacro> <postcommands>False</postcommands> <endlinechar>10</endlinechar> </options> </connection> END ; open (my $resultfile, '>>', 'resultxml.xml') or die "Can't open file: +$!"; #this is the wrapper my $config= <<END <?xml version="1.0" encoding="UTF-8"?> <!-- The following lines can be modified at your own risks. --> <configuration version="0.2.1.4" savepassword="True"> <root type="database" name="WSSS" expanded="True"> <container type="folder" name="CM" expanded="True"> <container type="folder" name="1-AD/M" expanded="True"> </container> </container> </root> </configuration> END ; #start an xml structure with the outer wrapper my $tree=XML::TreeBuilder->new(); $tree->parse($config); # find the insert location my $container=$tree->look_down("_tag"=>"container", "name"=>"1-AD/M"); my $csv=Text::CSV->new(); open my $fh, "<:encoding(utf8)", $paramsfile or die "$paramsfile: $!"; # loop through the lines of the CSV and copy the connection structure +into # the wrapper with the updated values while(my $row=$csv->getline($fh)){ my $contree=XML::TreeBuilder->new(); #make an xml structure of th +e connection $contree->parse($connection); $container->push_content($contree); #insert it into the wrapper $contree->attr('name',$row->[0]); #set the name attribute $container->push_content("\n "); #make it a little prettier my $index=0; for my $tagname(@taglist){ #loop through the columns my $tag=$contree->look_down('_tag'=> $tagname); $tag->push_content($row->[$index++]); } } #print '<?xml version="1.0" encoding="UTF-8"?>'."\n"; print $tree->as_XML(); print $resultfile '<?xml version="1.0" encoding="UTF-8"?>'."\n"; print $resultfile $tree->as_XML(); close($resultfile);

      If it doesn't have the container closing tags then it won't be valid XML (which is certainly possible for a config file, though kind of rude of them to do it that way). No decent XML generator will let you do that. A straightforward way to remove them is to do something like:

      $mycontainerless=$tree->as_XML(); $mycontainerless=~%</container>%%gi;

      before you write it to the file.

        Thanks bitingduck. i tried the why you said above but seems some error while running the script. not able to resolve the error. can you please advise where am doing wrong. Main script :
        #!/usr/bin/perl use warnings; use strict; use Text::CSV; use XML::TreeBuilder; use Data::Dumper; my $xmlbase='original.xml'; my $paramsfile = 'params.csv'; my $mycontainerless; # the list of tags filled from the csv file my @taglist=qw(name protocol host commandline); # this is the part that repeats, so we break it out separately # the tags that get filled from the CSV file are left empty my $connection= <<END <connection type="PuTTY" name="WSSS"> <connection_info> <name></name> <protocol></protocol> <host></host> <port>22</port> <session>Default Settings</session> <commandline></commandline> <description /> </connection_info> <login> <login /> <password /> <prompt /> </login> <timeout> <connectiontimeout>1000</connectiontimeout> <logintimeout>750</logintimeout> <passwordtimeout>750</passwordtimeout> <commandtimeout>750</commandtimeout> </timeout> <command> <command1 /> <command2 /> <command3 /> <command4 /> <command5 /> </command> <options> <loginmacro>False</loginmacro> <postcommands>False</postcommands> <endlinechar>10</endlinechar> </options> </connection> END ; open (my $resultfile, '>>', 'resultxml.xml') or die "Can't open file: +$!"; #this is the wrapper my $config= <<END <?xml version="1.0" encoding="UTF-8"?> <!-- The following lines can be modified at your own risks. --> <configuration version="0.2.1.4" savepassword="True"> <root type="database" name="WSSS" expanded="True"> <container type="folder" name="CM" expanded="True"> <container type="folder" name="1-AD/M" expanded="True"> </container> </container> </root> </configuration> END ; #start an xml structure with the outer wrapper my $tree=XML::TreeBuilder->new(); $tree->parse($config); # find the insert location my $container=$tree->look_down("_tag"=>"container", "name"=>"1-AD/M"); my $csv=Text::CSV->new(); open my $fh, "<:encoding(utf8)", $paramsfile or die "$paramsfile: $!"; # loop through the lines of the CSV and copy the connection structure +into # the wrapper with the updated values while(my $row=$csv->getline($fh)){ my $contree=XML::TreeBuilder->new(); #make an xml structure of th +e connection $contree->parse($connection); $container->push_content($contree); #insert it into the wrapper $contree->attr('name',$row->[0]); #set the name attribute $container->push_content("\n "); #make it a little prettier my $index=0; for my $tagname(@taglist){ #loop through the columns my $tag=$contree->look_down('_tag'=> $tagname); $tag->push_content($row->[$index++]); } } #print '<?xml version="1.0" encoding="UTF-8"?>'."\n"; print $tree->as_XML(); $mycontainerless=$tree->as_XML(); $mycontainerless=~%</container>%%gi; print $mycontainerless; print $resultfile '<?xml version="1.0" encoding="UTF-8"?>'."\n"; print $resultfile $tree->as_XML(); close($resultfile);
        Error :
        >perl xml.pl Bareword found where operator expected at xml.pl line 98, near "%%gi" (Missing operator before gi?) Operator or semicolon missing before %gi at xml.pl line 98. Ambiguous use of % resolved as operator % at xml.pl line 98. syntax error at xml.pl line 98, near "%%gi" Execution of xml.pl aborted due to compilation errors.
        line number 98 is :
        $mycontainerless=~%</container>%%gi;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1006444]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2014-07-31 01:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (244 votes), past polls