in reply to Reading XML File Hash in text
Semi-tested (I don't know Text::CSV so I might not be using it as well as possible). You'll be much happier if you use some more robust approaches to XML (XML::LibXML), CSV, and file handling. This might not be final but it's worth playing around with the pieces and approach.
use strict; use warnings; use XML::LibXML; use Text::CSV; open my $dip_file, ">>", "C:/director/DIPFILE.TXT" or die "Couldn't open DIP file for appending: $!"; open my $dip_log, ">>", "C:/director/DIPLOG.TXT" or die "Couldn't open DIP log for appending: $!"; my $csv = Text::CSV->new; # glob is a built-in, you don't need to "use File::Glob." my @dipfile = glob('C:/director/*.xml'); my $parser = XML::LibXML->new(); # set options for $parser here if needed, like $parser->recover(1) for # messed up XML. for my $xml_file ( @dipfile ) { my $doc = $parser->parse_file($xml_file); my ( $default_name ) = $doc->findnodes("//defaultName"); my ( $default_description ) = $doc->findnodes("//defaultDescriptio +n"); my ( $report_execution_time ) = $doc->findnodes("//reportExecution +Time"); $csv->combine( $xml_file, $default_name ? $default_name->textContent : "", $default_description ? $default_description->textCo +ntent : "", $report_execution_time ? $report_execution_time->te +xtContent : "", ); # Update: print $csv->string, "\n"; # To see it in the terminal. $dip_file->print( $csv->string, "\n" ); $dip_log->print( $csv->string, "\n" ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Reading XML File Hash in text
by drodinthe559 (Monk) on May 22, 2009 at 22:49 UTC |