I am fairly new to perl and am trying to write a Perl script to parse 1 large xml file containing many records into many xml files containing 1 record each. The records are between <rdf: Description> tags.
So far, I have a script that reads in an infile and a directory and creates an outfile in the specified directory to which "print OUTFILE ("New Information"); can add new information (below)
Can anyone give me suggestions on any of the following?
1. reading from the infile and writing to the outfile
2. parsing the xml text using the <rdf: Description> tags
3. Saving the xml header (the <?xml version="1.0" encoding="UTF-8"?>) and attaching it to the beginning of each of the individual records
4. using the file name listed under <dc:identifier> as the outfile name instead of the record count?
Thanks!
#!/usr/bin/perl -w
# Sort out the input and output files
my ($infile, $dir) = @ARGV;
defined($infile) && defined($dir) || usage();
$infile && $dir || usage();
$record_count = 0;
$record_element = 'rdf

escription';
$filename_element = 'dc:identifier';
#Create Infile
if (@ARGV > 0)
{
open(INFILE, "<$ARGV[0]") || die "Failed to open $ARGV[0]\n";
$infile = "INFILE";
}
else { $infile = "STDIN"; }
#Create Outfile
if (@ARGV > 1)
{
open(OUTFILE, ">$ARGV[1]/$record_count.xml") || die "Failed to open $ARGV[1]/$record_count.xml\n";
$outfile = "OUTFILE";
}
else { $outfile = "STDOUT"; }
else
print OUTFILE ("New Information");
#$stop = ((INFILE == $record_element) || (INFILE == eof));
#if ($stop)
close (INFILE);
close (OUTFILE);
sub msg {
print @_, "\n";
}
sub usage {
msg("Usage: $0 <file> <directory>");
exit(1);
}