in reply to XML::XPath and processing multiple files

foreach $page (@files) { my $xp = XML::XPath->new(filename => $page);

The salient bit here would be how you are generating @files. You say it is a "simple text file", but you don't show the code you use to read it in. Did you perhaps forget to chomp the lines? What's in the "..." in "Cannot open file..." (an important diagnostic message, perhaps)? As Corion suggests, can you guarantee the files in the list are actually valid to begin with?

Replies are listed 'Best First'.
Re^2: XML::XPath and processing multiple files
by ATLien (Initiate) on Jul 15, 2013 at 14:47 UTC
    My apologies for not being complete. The file list is a manually created list of selected file paths. As far as whether or not they exist - if I pair the list down to just the first file - the script works fine. Here is the entire script:
    use XML::XPath; use XML::XPath::XMLParser; my $filelist = shift; #Process file list open(FILELIST, "$filelist") or die("Unable to open file"); my @files = <FILELIST>; close(FILELIST); foreach $page (@files) { my $xp = XML::XPath->new(filename => $page); my $nodeset = $xp->find('//DCR'); # find all DCRs my @nodelist = $nodeset->get_nodelist; #@dcrs = map($_->string_value, @nodelist); @dcrs = map {$_->string_value ? $_->string_value : ()} @nodeli +st; foreach my $dcr(@dcrs) { print "$dcr\n"; } }
    For some reason I thought I'd added chomp before and it didn't work:
    chomp($page);

      Most likely there is other whitespace at the end of your file names. Or maybe the "first" file exists in two places.

      Does the script still fail if you have the line with the first, existing file duplicated?

        Yes it does fail with the first duped. It only works when there is one file in the list. Weird I know.
      chomp may not work if the filelist was manually created on a windows machine and you are running the script on unix. Suggest you try a regex instead.
      open(IN,'file.txt') or die "$!"; my @files = <IN>; foreach $page (@files) { $page =~ s/\s+$//g; print "parsing [$page]\n"; # code }
      poj
      Also the complete error is:

      Cannot open file 'file1.xml ' at /apps/interwoven/TeamSite/iw-perl/vendor/lib/XML/XPath.pm line 53.

        Did you note the whitespace between the .xml and the trailing apostrophe in the error message? Maybe your file names do not end with whitespace in the extension?