File: marc2xml

package info (click to toggle)
libmarc-xml-perl 0.93-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 224 kB
  • sloc: xml: 1,159; perl: 406; makefile: 2
file content (69 lines) | stat: -rwxr-xr-x 1,266 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl

=head1 NAME

marc2xml - convert a MARC file to XML

=head1 SYNOPSIS

    # convert a file 
    % marc2xml marc.dat > marc.xml

    # or use in a pipeline
    % cat marc.dat | marc2xml > marc.xml

=head1 DESCRIPTION

marc2xml is a command line utility for converting MARC21 bibliographic
data to XML using the Library of Congress Slim Schema. Conversion is 
handled using the MARC::Record and MARC::File::XML packages.

=cut

use strict;
use warnings;
use MARC::Record;
use MARC::Batch;
use MARC::File::XML;
use IO::File;

my $file = shift;
my $fh; 

## read from a file?
if ( $file ) { 
    fatal( "no such file: $file" ) if ! -r $file;
    $fh = IO::File->new( $file );
} 

## or from STDIN?
else {
    $fh = \*STDIN;
}

## set up intput/output for utf8 encoding
binmode( STDOUT, ':utf8' );
binmode( $fh, ':bytes' );

my $found = 0;
my $batch = MARC::Batch->new( 'USMARC', $fh );

# be lenient, it's MARC afterall :)
$batch->warnings_off();
$batch->strict_off();

while ( my $record = $batch->next() ) { 
    if ( ! $found ) { 
        print MARC::File::XML::header();
        $found = 1;
    }
    print MARC::File::XML::record( $record );
}

print MARC::File::XML::footer() if $found;

sub fatal {
    print STDERR shift, "\n";
    exit( 1 );
}