Hi, I'm using xml::writer trying to output the xml file from the csv input file. I'm not sure how to print the name as a separate tag.
#input file name subname start size CAL_M reserved 0 13 CAL_M o_check_out 13 7 CAL_M o_ttr_first 20 4 CAL_M o_cal_check_1 24 4 PSQN_0 reserved 0 8 PSQN_0 check_final 8 10 PSQN_0 o_check 18 10
#expected output file <structure name="CAL_M"> <value type = "13"name = "reserved" start = "0" </value> <value type = "7" name = "o_check_out" start = "13"</value> <value type = "4" name = "o_ttr_first" start = "20"</value> <value type = "4" name = "o_cal_check_1"start = "24"</value> </structure> <structure name="PSQN_0"> <value type = "8" name = "reserved" start = "0" </value> <value type = "10" name = "check_final"start = "8"</value> <value type = "10" name = "o_check" start = "18" </value> </structure>
#current output file <value type = "13"name = "reserved" start = "0" </value> <value type = "7" name = "o_check_out" start = "13"</value> <value type = "4" name = "o_ttr_first" start = "20"</value> <value type = "4" name = "o_cal_check_1"start = "24"</value> <value type = "8" name = "reserved" start = "0" </value> <value type = "10" name = "check_final"start = "8"</value> <value type = "10" name = "o_check" start = "18" </value>
Here's my code
#input file open (FILE,"<in.csv") || die "cannot open file: $!"; my @array =<FILE>; close (FILE); #output file my $output_file = "myfile.xml"; my $output = new IO::File(">$output_file"); my $writer = new XML::Writer(OUTPUT=>$output, DATA_MODE=>1, DATA_INDEN +T => 2); #structure foreach my $item(@array) { chomp($item); $item =~/(\w+),(\w+),(\d+),(\d+)/; my $name =$1; my $sname=$2; my $start=$3; my $size =$4; $writer->startTag("structure", "name"=>$name); $writer->startTag("value", "type"=>$size, "name"=>$sname, "start"= +>$start); $writer->endTag(); $writer->endTag(); } $writer->end(); $output->close();

In reply to perl writing to xml file (xml::writer) by analys

Title:
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, details, 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, summary, 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.