LF has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I am simply trying to read all the (XML) files from within a directory and make a few text changes to each:

The changes are meant to change all occurrances of XML lines like:

<dc:subject xml:lang="en-US">18th century 17th century</dc:subject>

To:

<dc:subject xml:lang="en-US">17th century</dc:subject>
<dc:subject xml:lang="en-US">18th century</dc:subject>

And then to output the files to a new directory.

However, it seems like there are some errors in my code, esp. for opening/closing the files. Can anyone tell me what is wrong? This is my code:

#!/usr/bin/perl -w use warnings; use strict; my $record_element = 'dcterms:available'; my $filename_element = 'dc:identifier'; my ($file, $dir) = @ARGV; defined($file) && defined($dir) || usage(); chop($dir) if $dir =~ m#/$#; #$/ = "</$record_element>"; mkdir $dir; open(INPUT, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n"; $file = "INPUT"; my $record_count = 0; my $header1 = '<?xml version="1.0" encoding="UTF-8"?>'; my $header2 = '<qualifieddc xmlns="http://example.org/appqualifieddc/" +'; my $header3 = 'xmlns:dc="http://purl.org/dc/elements/1.1/"'; my $header4 = 'xmlns:dcterms="http://purl.org/dc/terms/"'; my $header5 = 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'; my $header6 = 'xsi:schemaLocation="http://example.org/appqualifieddc/' +; my $header7 = 'http://dublincore.org/schemas/xmls/qdc/2003/04/02/appqu +alifieddc.xsd"'; my $closing = '</qualifieddc>'; $/ ='terms/">'; <INPUT>; $/ = "</$record_element>"; while(<INPUT>){ next if m/^\s*$/; #Disregard 0+ trailing spaces s/^\s*//; #Collapse 0+ leading spaces $record_count++; if (m#<$filename_element xsi:type="dcterms:URI">(\w+) (\w+) (\w+)_(\ +w+)</$filename_element>#s) { msg("Just matched $1 $2 $3_$4"); my $outfile = "$dir/$4.xml"; open (OUTPUT, '>', $outfile) or die "Can't create/open $outfile fo +r writing"; print OUTPUT $header1, "\n", $header2, "\n", $header3, "\n", $ +header4, "\n", $header5, "\n", $header6, "\n", $header7, "\n"; print OUTPUT $_, "\n"; print OUTPUT $closing; close (OUTPUT) or die "Couldn't close $outfile after writing recor +d"; } else { warn("Couldn't find filename in record $record_count, skipping..." +); } } close (INPUT) or die "Couldn't close $file"; close (OUTFILE); $record_count--; msg("Processed $record_count records from $file"); sub msg { print @_, "\n"; } sub usage { msg("Usage: $0 <file> <directory>"); exit(1); }

Replies are listed 'Best First'.
Re: Read/Change files in a Directory
by osunderdog (Deacon) on Nov 09, 2004 at 17:51 UTC

    Lacking a few details, but here is how I would approach this:

    use strict; use XML::Simple; my $xs = XML::Simple->new(KeepRoot=>1); my $data; { local $/; $data = <DATA>; } my $inStruct = $xs->XMLin($data); my $outStruct; foreach my $tag (@{$inStruct->{Root}->{'dc:subject'}}) { my @matches = $tag->{content} =~ /(\d+th century)/g; foreach my $match (@matches) { my %item = %$tag; $item{content} = $match; push(@{$outStruct->{Root}->{'dc:subject'}}, \%item); } } my $resultXML = $xs->XMLout($outStruct); print $resultXML; __DATA__ <Root> <dc:subject xml:lang="en-US">18th century 17th century</dc:subject> <dc:subject xml:lang="en-US">18th century 14th century</dc:subject> <dc:subject xml:lang="en-US">18th century 12th century</dc:subject> <dc:subject xml:lang="en-US">18th century 11th century</dc:subject> <dc:subject xml:lang="en-US">18th century 10th century</dc:subject> <dc:subject xml:lang="en-US">18th century 12th century</dc:subject> <dc:subject xml:lang="en-US">18th century 12th century 12th century 12 +th century 12th century</dc:subject> </Root>

    "Look, Shiny Things!" is not a better business strategy than compatibility and reuse.


    OSUnderdog
Re: Read/Change files in a Directory
by zigdon (Deacon) on Nov 09, 2004 at 17:25 UTC

    I must be terribly confused - but from what I can decode in your program, it does nothing like what you described as your goal? Perhaps you can post a short example file, or explain more what you're trying to do?

    Since I can't understand what you're trying to do, I'll just give you the one tip - whenever you find yourself using variables like $header1...$header7, you probably will be better off using an array @headers. Or, if this is really your whole program, all the headers could be put in a single scalar $header.

    -- zigdon

Re: Read/Change files in a Directory
by bgreenlee (Friar) on Nov 09, 2004 at 17:21 UTC

    It's hard to troubleshoot your code without more information about what errors you're getting, but if you're really just making a simple change like that, and this is a one-off thing, this sounds like a good candidate for a little perl-one-liner:

    perl -pi -0777 -e "s{(<dc:subject xml:lang=\"en-US\">)(\w+) (\w+) (\w+ +) (\w+)(</dc:subject>)}{$1$2 $3$6\n$1$4 $5$6}gs" *.xml

    Experiment with this on a backup copy of the directory you're working with, as it will change the files in place.

    -b