Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

XML::Writer error message

by vinoth.ree (Monsignor)
on Jul 07, 2009 at 06:26 UTC ( [id://777753]=perlquestion: print w/replies, xml ) Need Help??

vinoth.ree has asked for the wisdom of the Perl Monks concerning the following question:

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.

Replies are listed 'Best First'.
Re: XML::Writer error message
by pKai (Priest) on Jul 07, 2009 at 07:37 UTC

    You need to set up the top level structure of your output document outside of the while loop.

    Also, even if this is nothing which fails your code at the moment, you should also concider using a hash instead of evaling symbolic variable names.

    use strict; use warnings; use XML::Writer; #use XML::Reader; use IO::File; open(my $fh,"./input.txt") or die "Cannot open input file."; 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); $writer->xmlDecl("UTF-8"); #XML declaration $writer->startTag("data"); # The root tag while(my $input=<$fh>){ chomp($input); next if !$input; my %vals; @vals{@array} = split /:/, $input; $writer->startTag("row"); # enclosing tag for every data row for my $name (@array) { $writer->startTag($name); $writer->characters($vals{$name}); $writer->endTag(); } $writer->endTag(); # = endTag("row") } $writer->endTag(); # = endTag("data") $writer->end(); $output->close();

    Update: Typically you want some structure in the XML document, to address whole data rows. Added the "row" tag level for that.

Re: XML::Writer error message
by Anonymous Monk on Jul 07, 2009 at 06:32 UTC
    This is basic structure of your code
    open outfile while( readline file){ close outfile }
    After you close outfile, it is not open, you cannot write to it.

      No, it's not. Or at least it wasn't 1 hour after the question was originally posted, when I logged into PM and saw the question. Updating nodes without notice is bad.

      It's a logical thing with the generated XML. There is no single root tag in there as well-formed XML requires. So when the 2nd ("root") startTag is requested the error is thrown, because there can only be one.

        Yes it was. ree has again updated his node without notice.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://777753]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (6)
As of 2024-03-28 11:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found