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 = 'rdfescription';
$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 $A
+RGV[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);
}
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.