Current location: Hot Scripts Forums » Programming Languages » Perl » Perl Script to parse 1 xml file into many


Perl Script to parse 1 xml file into many

Reply
  #1 (permalink)  
Old 08-09-04, 10:21 AM
flsh flsh is offline
New Member
 
Join Date: Aug 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Perl Script to parse 1 xml file into many

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 $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);
}
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
crontab not executing a perl script abtimoteo Perl 5 05-27-08 11:42 AM
Help with my largest perl script. Grabbing data from a file. Sunnmann Perl 2 04-23-08 03:27 AM
Is there any integrity of script rankings? webmaster@atmanager.com Hot Scripts Forum Questions, Suggestions and Feedback 17 08-06-04 12:12 AM
Upload file to table so ONLY files tied to primary key are displayed in record? grafixDummy PHP 4 12-20-03 04:28 PM
ZIP/RAR file comment/.diz/.nfo viewer script net4ward Script Requests 0 11-19-03 04:41 PM


All times are GMT -5. The time now is 03:58 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.