Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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 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="" 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);

In reply to Re^4: 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

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

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (8)
    As of 2018-06-24 22:37 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (126 votes). Check out past polls.