Re: Converting XML to MARC without reading a file?

[email protected] (David Cook) Thu, 19 Apr 2018 10:44:57 +1000
Newsgroups perl.perl4lib
Message-ID <CACVmdZR5jsnxyFNDHRVRmjH4DhYDiWfKPacjCWAdZB9H4064dA@mail.gmail.com>
--0000000000007a8284056a28e1e6
Content-Type: text/plain; charset="UTF-8"

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 <[email protected]> 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
>
>

--0000000000007a8284056a28e1e6
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Hey Andy,<div><br></div><div>That does sound like an annoy=
ing problem. Someone else might have a better answer, but I think I have a =
clever solution...<br><br>Instead of using MARC::File::XML directly, you ca=
n use MARC::Batch instead:<br><br>my $batch =3D MARC::Batch-&gt;new( &#39;X=
ML&#39;, $filename );<br><br>According to the MARC::Batch documentation, yo=
u can actually use a file handle instead of a file name:=C2=A0<a href=3D"ht=
tp://search.cpan.org/~gmcharlt/MARC-Record-2.0.7/lib/MARC/Batch.pm#new(_$ty=
pe,_@files_)">http://search.cpan.org/~gmcharlt/MARC-Record-2.0.7/lib/MARC/B=
atch.pm#new(_$type,_@files_)</a>.=C2=A0</div><div><br></div><div>You should=
 be able to create an &quot;in-memory&quot; file handle that points to your=
 scalar variable holding your XML. Like so:=C2=A0</div><div><br></div><div>=
open(my $fh, &quot;&lt;&quot;, \$variable);</div><div><br></div><div>You sh=
ould be able to pass in the $fh to MARC::Batch like this:<br><br>

<span style=3D"color:rgb(34,34,34);font-family:arial,sans-serif;font-size:s=
mall;font-style:normal;font-variant-ligatures:normal;font-variant-caps:norm=
al;font-weight:400;letter-spacing:normal;text-align:start;text-indent:0px;t=
ext-transform:none;white-space:normal;word-spacing:0px;background-color:rgb=
(255,255,255);text-decoration-style:initial;text-decoration-color:initial;f=
loat:none;display:inline">my $batch =3D MARC::Batch-&gt;new( &#39;XML&#39;,=
 $fh );</span>

<br></div><div><br></div><div>Now I haven&#39;t tried this... but in theory=
 it should work.=C2=A0</div></div><div class=3D"gmail_extra"><br><div class=
=3D"gmail_quote">On Thu, Apr 19, 2018 at 10:25 AM, Andy Kohler <span dir=3D=
"ltr">&lt;<a href=3D"mailto:[email protected]" target=3D"_blank">akohler=
[email protected]</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quote" st=
yle=3D"margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div =
dir=3D"ltr"><div><div><div><div>Hi -<br><br></div>I&#39;m pulling records f=
rom the WorldCat Search API in MARCXML, and need to convert them to binary =
MARC for further evaluation, which I&#39;ll do via MARC::Record.<br><br></d=
iv>Problem: Converting from MARCXML via MARC::File::XML seems to require re=
ading the records from a file.=C2=A0 I&#39;ve already got the XML stored in=
 a variable, retrieved via LWP::Simple-&gt;get().<br><br></div>Do I have to=
 write the XML to a file, then read it in again to convert it?=C2=A0 Or am =
I just missing something obvious?<br><br></div><div>I&#39;ve tried things l=
ike:<br></div><div>$xml =3D get($api_call); # also verified that $xml now c=
ontains MARCXML for 1 or more records<br></div><div>my $batch =3D=C2=A0 MAR=
C::File::XML-&gt;in($xml);<br>while (my $record =3D $batch-&gt;next()) {<br=
>=C2=A0 print $record;<br>}<br></div><div>but I get the error: Can&#39;t ca=
ll method &quot;next&quot; on an undefined value<br><br></div><div>Thanks -=
-Andy<br><br></div></div>
</blockquote></div><br></div>

--0000000000007a8284056a28e1e6--