in reply to Re: TWIGging XML to CSV: what's wrong with my syntax?
in thread TWIGging XML to CSV: what's wrong with my syntax?
Thanks toolic! I went with your modification and extended it a bit to include a couple more conditions.
I've added a file handle to print the data to a file rather than to STOUT; however, it is not working correctly, as instead of getting a file with a list of town names I get a a full set of tags for 1 XML element (probably the last element in the XML file). The STDOUT generated by the print statement inside the twig is correct.use warnings; use strict; use XML::Twig; my $source_file = 'datafile_towns.xml'; my $out_file = 'illytowns.xml'; my $twig = XML::Twig->new( twig_handlers => { town => \&town }, ); $twig->parsefile($source_file); open (my $fh_out, '>', $out_file) or die "unable to open '$out_file' f +or writing: $!"; sub town { my ($twig, $town) = @_; my $townx = $town->first_child('location')->first_child('mapx')->t +ext; my $towny = $town->first_child('location')->first_child('mapy')->t +ext; if (($townx > 556) and ($towny > -2502) and ($towny < -2214)) { print $town->first_child('towndata')->first_child_text('townna +me'); print "\n"; } $twig->print($fh_out); $twig->purge; }
|
|---|