Reputation:
I'm new here and I need help, I'm not a good programmer but I debrouille more in the development of web pages (Java). malheuereusement I have not been able to find an internship position in this field, but I have found in each other area having no other choice, I can accept that, unfortunately my training master is not a programmer, he is a director of System and it is even I who made him discover some thing in perl. come in on my Problem: I was able to Implement a perl-script that parses the file-xml, but for that I must go to my editor perl write the names of all my files, unfortunately I have thousands of files, xml, and I ' love that this code has to be able to find all-xml files located in a folder and the following naturement parser without any time I began to write my editor the name of thousands of files. that's my problem. If I possessed in 4 or 6 files, I have written the names in my editor-perl and I will have received a result in the immediate
I can somehow summarizes thus:
1- (Extracts XML Perl-script) saved in a folder on my PC stuff 2- Perl-script searches the XML files in the folder 3- perl-script processes (parse) the fichiers_xml 4- perl-script returns me the result of parsing a text file
here is the code I paser für implements, it works well
use strict;
use warnings;
use XML::Twig;
use File::Find;
my $file1 = shift or die 'No file1';
my @files = @ARGV or die 'No files';
my $FileResult = 'result.csv';
open( my $FhResult, '>', $FileResult )or die ("Unable to open file $FileResult\n$!");
my $twig1= XML::Twig->new(
twig_handlers => {
'Parameter' => sub {
my $attr_name = $_->{'att'}->{'name'} // 'fault';
print $FhResult $attr_name . ",";
},
},
);
print $FhResult( (split('_', $file1,2))[0] . ',' );
$twig1->parsefile($file1);
for my $file (@files) {
my $twig2 = XML::Twig->new(
twig_handlers => {
'Parameter' => sub {
my $attr_value = $_->{'att'}->{'value'} // 'fault';
print $FhResult $attr_value . ",";
},
},
);
print $FhResult ( split( '_', "\n$file", 2 ) )[0] . ',';
$twig2->parsefile($file);
}
close $FhResult;
Upvotes: 0
Views: 175
Reputation: 3484
If I understand your question correctly, you want a way to find all of the XML files in a directory.
Here's one way:
chdir $the_directory_we_want_to_process;
foreach my $xml_file ( glob('*.xml') ) {
process_the_file($xml_file);
}
the glob('*.xml')
function performs a shell pattern match to generate a list of the names of all the files in the current directory ending in '.xml'
Here's another:
opendir(DH, $the_directory_we_want_to_process)
while ( $file = readdir(DH) ) {
next unless /\.xml$/;
process_the_file($xml_file);
}
closedir(DH);
Upvotes: 2