我想解析位於一個目錄的子目錄中的特定XML文件。出於某種原因,我收到錯誤說文件不存在。如果文件不存在,它應該轉到下一個子目錄。更改目錄並獲取xml文件以解析perl中的某些數據
這裏是我的代碼
use strict;
use warnings;
use Data::Dumper;
use XML::Simple;
my @xmlsearch = map { chomp; $_ } `ls`;
foreach my $directory (@xmlsearch) {
print "$directory \n";
chdir($directory) or die "Couldn't change to [$directory]: $!";
my @findResults = `find -name education.xml`;
foreach my $educationresults (@findResults){
print $educationresults;
my $parser = new XML::Simple;
my $data = $parser->XMLin($educationresults);
print Dumper($data);
chdir('..');
}
}
ERROR
music/gitar/education.xml
File does not exist: ./music/gitar/education.xml
爲什麼不使用['使用File :: Find;'](http://perldoc.perl.org/File/Find.html)? – 2012-06-08 15:25:14
您是否想要遞歸查找'education.xml'文件,還是僅查找這些目錄? – TLP
你好,我只是想找到一個教育目錄到一個目錄。所以主目錄是音樂,可以說10個子目錄。例如吉他,鋼琴,鼓。我只想在音樂/吉他或音樂/鋼琴下搜索。我不想在音樂/吉他/ dir1/dir2下搜索。 – Maxyie