I've just tested my theory, and it worked great. Here's some code that you can try at home:
#!/usr/bin/perl use strict; use warnings; use MARC::File::XML; use MARC::Batch; my $xml; while (my $line = <DATA>){ $xml .= $line; } open(my $fh, "<", \$xml) or die "Couldn't open file handle: $!"; my $batch = MARC::Batch->new( 'XML', $fh ); while (my $marc = $batch->next() ){ warn $marc->subfield(245,"a"); } __DATA__ <?xml version="1.0" encoding="UTF-8" ?> <collection> <record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd" xmlns="http://www.loc.gov/MARC21/slim"> <leader>00740nam a2200253 a 4500</leader> <datafield tag="245" ind1="1" ind2="0"> <subfield code="a">Test 1</subfield> </datafield> </record> <record xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd" xmlns="http://www.loc.gov/MARC21/slim"> <leader>00898nam a2200265 a 4500</leader> <datafield tag="245" ind1="0" ind2="0"> <subfield code="a">Test 2</subfield> </datafield> </record> </collection> On Thu, Apr 19, 2018 at 10:44 AM, David Cook <kingandcapt...@gmail.com> wrote: > Hey Andy, > > That does sound like an annoying problem. Someone else might have a better > answer, but I think I have a clever solution... > > Instead of using MARC::File::XML directly, you can use MARC::Batch instead: > > my $batch = MARC::Batch->new( 'XML', $filename ); > > According to the MARC::Batch documentation, you can actually use a file > handle instead of a file name: http://search.cpan.org/~ > gmcharlt/MARC-Record-2.0.7/lib/MARC/Batch.pm#new(_$type,_@files_). > > You should be able to create an "in-memory" file handle that points to > your scalar variable holding your XML. Like so: > > open(my $fh, "<", \$variable); > > You should be able to pass in the $fh to MARC::Batch like this: > > my $batch = MARC::Batch->new( 'XML', $fh ); > > Now I haven't tried this... but in theory it should work. > > On Thu, Apr 19, 2018 at 10:25 AM, Andy Kohler <akohler...@gmail.com> > wrote: > >> Hi - >> >> I'm pulling records from the WorldCat Search API in MARCXML, and need to >> convert them to binary MARC for further evaluation, which I'll do via >> MARC::Record. >> >> Problem: Converting from MARCXML via MARC::File::XML seems to require >> reading the records from a file. I've already got the XML stored in a >> variable, retrieved via LWP::Simple->get(). >> >> Do I have to write the XML to a file, then read it in again to convert >> it? Or am I just missing something obvious? >> >> I've tried things like: >> $xml = get($api_call); # also verified that $xml now contains MARCXML for >> 1 or more records >> my $batch = MARC::File::XML->in($xml); >> while (my $record = $batch->next()) { >> print $record; >> } >> but I get the error: Can't call method "next" on an undefined value >> >> Thanks --Andy >> >> >