Beefy Boxes and Bandwidth Generously Provided by pair Networks Cowboy Neal with Hat
XP is just a number
 
PerlMonks  

Re: XML::Writer error message

by pKai (Priest)
on Jul 07, 2009 at 07:37 UTC ( #777771=note: print w/ replies, xml ) Need Help??


in reply to XML::Writer error message

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.


Comment on Re: XML::Writer error message
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (8)
As of 2014-04-18 23:31 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (473 votes), past polls