Re: Converting XML to MARC without reading a file?
[email protected] (Andy Kohler) Wed, 18 Apr 2018 20:54:49 -0800
| Newsgroups | perl.perl4lib |
|---|---|
| Message-ID | <CAGZXaBNRJKDi50Ek3OH3ziBUh+SG6Rz81sqw2eyV70fhQW9DGQ@mail.gmail.com> |
--94eb2c09533218d198056a2c5fe1
Content-Type: text/plain; charset="UTF-8"
Many thanks to David, Bruce and Karl for suggestions - all were helpful!
David and Bruce both reminded me that scalar variables can be treated as
file handles:
open(my $fh, "<", \$xml) or die "Couldn't open file handle: $!";
my $batch = MARC::Batch->new( 'XML', $fh );
I had tried this without luck earlier. David kindly sent a complete
example - which worked in isolation, but failed in the context of my
program with a frustrating "Couldn't open file handle: Invalid argument"
until I realized I'd forgotten the essential "use warnings"... which gave
me the frustrating but much more helpful:
"Strings with code points over 0xFF may not be mapped into in-memory file
handles"
A moment with Google found pages like this:
https://bugzilla.redhat.com/show_bug.cgi?id=1048324
which helped me realize that the XML I was retrieving from WorldCat really
was encoded as UTF-8, which I hadn't accounted for so far.
Wrapping my retrieved XML like so:
utf8::encode($contents);
has resolved the problem, so far at least - have to test more with records
with diacritics to be sure. But this helps me move forward.
Thanks again! --Andy
On Wed, Apr 18, 2018 at 4:25 PM, 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
>
>
--94eb2c09533218d198056a2c5fe1
Content-Type: text/html; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
<div dir=3D"ltr"><div><div><div>Many thanks to David, Bruce and Karl for su=
ggestions - all were helpful!<br><br></div>David and Bruce both reminded me=
that scalar variables can be treated as file handles:<br>
<div>=C2=A0=C2=A0=C2=A0 open(my $fh, "<", \$xml) or die "=
Couldn't open file handle: $!";</div><span class=3D"gmail-im"><div=
>=C2=A0=C2=A0=C2=A0 my $batch =3D MARC::Batch->new( 'XML', $fh )=
;<br><br></div><div>I had tried this without luck earlier.=C2=A0 David kind=
ly sent a complete example - which worked in isolation, but failed in the c=
ontext of my program with a frustrating "Couldn't open file handle=
: Invalid argument" until I realized I'd forgotten the essential &=
quot;use warnings"... which gave me the frustrating but much more help=
ful:<br><br>"Strings with code points over 0xFF may not be mapped into=
in-memory file handles"<br><br></div><div>A moment with Google found =
pages like this:<br><a href=3D"https://bugzilla.redhat.com/show_bug.cgi?id=
=3D1048324">https://bugzilla.redhat.com/show_bug.cgi?id=3D1048324</a><br><b=
r></div><div>which helped me realize that the XML I was retrieving from Wor=
ldCat really was encoded as UTF-8, which I hadn't accounted for so far.=
<br><br></div><div>Wrapping my retrieved XML like so:<br>=C2=A0 utf8::encod=
e($contents);<br></div></span></div><br>has resolved the problem, so far at=
least - have to test more with records with diacritics to be sure.=C2=A0 B=
ut this helps me move forward.<br><br></div><div>Thanks again! --Andy<br><d=
iv><div><div><div><div class=3D"gmail_extra"><br><div class=3D"gmail_quote"=
>On Wed, Apr 18, 2018 at 4:25 PM, Andy Kohler <span dir=3D"ltr"><<a href=
=3D"mailto:[email protected]" target=3D"_blank">[email protected]</a>=
></span> wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0px=
0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><di=
v dir=3D"ltr"><div><div><div><div>Hi -<br><br></div>I'm pulling records=
from the WorldCat Search API in MARCXML, and need to convert them to binar=
y MARC for further evaluation, which I'll do via MARC::Record.<br><br><=
/div>Problem: Converting from MARCXML via MARC::File::XML seems to require =
reading the records from a file.=C2=A0 I've already got the XML stored =
in a variable, retrieved via LWP::Simple->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 a=
m I just missing something obvious?<br><br></div><div>I've tried things=
like:<br></div><div>$xml =3D get($api_call); # also verified that $xml now=
contains MARCXML for 1 or more records<br></div><div>my $batch =3D=C2=A0 M=
ARC::File::XML->in($xml);<br>while (my $record =3D $batch->next()) {<=
br>=C2=A0 print $record;<br>}<br></div><div>but I get the error: Can't =
call method "next" on an undefined value<br><br></div><div>Thanks=
--Andy<br><br></div></div>
</blockquote></div><br></div></div></div></div></div></div></div>
--94eb2c09533218d198056a2c5fe1--