perl script to search all files in folder -
i have 1 folder contains more number of xml files , extract specific information form xml files. used libxml extract wanted information 1 xml , succeded how can extract folder , each xml file using perl script. tried 1 xml file:
use warnings; use strict; use xml::libxml::reader; $file; open( $file, 'formal.xml'); $reader = xml::libxml::reader->new( io => $file ) or die ("unable open file"); %hash; while ($reader->nextelement( 'nuber' ) ) { $number = $reader->readinnerxml(); $reader->nextelement( 'data' ); $information = $reader->readouterxml(); $nums{$number}= $information; print( " number:$number\n" ); print( " information:$information\n" ); } print $num=keys%hash; close($file);
above code working , extracted want. need script search files in folder , extract same information files.
use file::find.
your code cannot working is. here untested script might want.
use warnings; use strict; use carp; use file::find; use file::spec::functions qw( canonpath ); use xml::libxml::reader; die "need directories\n" unless @argv; %hash; find( sub { $file = $_; $path = canonpath $file::find::name; return unless -f $path; return unless $file =~ /[.]xml\z/i; extract_information($path, \%hash); return; }, @argv ); use data::dumper; print dumper \%hash; sub extract_information { ($path, $hash) = @_; $ret = open $xmlin, '<', $path; unless ($ret) { carp "cannot open '$path': $!"; return; } $reader = xml::libxml::reader->new(io => $xmlin); unless ($reader) { carp "cannot create reader using '$path'"; return; } while ($reader->nextelement('number')) { $number = $reader->readinnerxml(); $reader->nextelement( 'data' ); $information = $reader->readouterxml(); $hash->{$path}{$number} = $information; } close $xmlin or carp "cannot close '$path': $!"; return; }
Comments
Post a Comment