I stared to learn XML::Writer and XML::Reader


use XML::Writer; use XML::Reader; use IO::File; open(FH,"./input.txt"); #open the input file my $input; my $output = new IO::File(">converted_to_xml.xml"); #file to write the + converted content my $writer = new XML::Writer(OUTPUT => $output); #write into the opene +d file my @array=qw(name emp_no designation salary); while($input=<FH>){ #Read the input line from the file chomp($input); #remove the newline character my($name,$emp_no,$designation,$salary)=split /:/, $input; #split t +he input by comma character $writer->xmlDecl("UTF-8"); #XML declaration foreach my $val(@array){ $writer->startTag("$val"); #Starting tag my $var="\$".$val; $writer->characters(eval("$var")); #Character data in an XM +L document $writer->endTag("$val"); } } $writer->end(); $output->close();

In this above code I was using the XML::Writer to generate the XML file like below for each employee.

<name>XXXX</name> <emp_no>XXXX<emp_no> <designation>XXX<designation> <salary>XXXX<salary>

When I execute the above code it says error message,

Attempt to insert start tag after close of document element at Write_to_XML.pl line 47

If I use the endTag after the foreach loop I won't get XML file with all employee, I get only first employee details in the XML file.


In reply to XML::Writer error message by vinoth.ree

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.