Re: Converting XML to MARC without reading a file?
[email protected] (David Cook) Thu, 19 Apr 2018 15:29:01 +1000
| Newsgroups | perl.perl4lib |
|---|---|
| Message-ID | <CACVmdZTxPoH112AZqR7q5JOAQRrVzU6bV3GwqDVk5KmNtbtp6Q@mail.gmail.com> |
--0000000000006137a8056a2cd9ab Content-Type: text/plain; charset="UTF-8" I'm glad that I was able to help! I had wondered if you were going to run into encoding issues, and I had thought about warning you... but I figured that was a different piece of the puzzle. So far, I've actually only written to in-memory file handles (my example above was the first time reading from one), and then used the bytes from the scalar variable, but I think all my data is ASCII so far. On Thu, Apr 19, 2018 at 2:54 PM, Andy Kohler <[email protected]> wrote: > 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 >> >> > --0000000000006137a8056a2cd9ab Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable <div dir=3D"ltr">I'm glad that I was able to help! I had wondered if yo= u were going to run into encoding issues, and I had thought about warning y= ou... but I figured that was a different piece of the puzzle. <br><br>So fa= r, I've actually only written to in-memory file handles (my example abo= ve was the first time reading from one), and then used the bytes from the s= calar variable, but I think all my data is ASCII so far.=C2=A0<div><br></di= v><div><br></div><div><br><div><br></div></div></div><div class=3D"gmail_ex= tra"><br><div class=3D"gmail_quote">On Thu, Apr 19, 2018 at 2:54 PM, Andy K= ohler <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:0 0 0 .8ex;border-left:1px #ccc solid;padd= ing-left:1ex"><div dir=3D"ltr"><div><div><div>Many thanks to David, Bruce a= nd Karl for suggestions - all were helpful!<br><br></div>David and Bruce bo= th reminded me that scalar variables can be treated as file handles:<span c= lass=3D""><br> <div>=C2=A0=C2=A0=C2=A0 open(my $fh, "<", \$xml) or die "= Couldn't open file handle: $!";</div></span><span class=3D"m_-9980= 58744160846443gmail-im"><span class=3D""><div>=C2=A0=C2=A0=C2=A0 my $batch = =3D MARC::Batch->new( 'XML', $fh );<br><br></div></span><div>I h= ad tried this without luck earlier.=C2=A0 David kindly sent a complete exam= ple - which worked in isolation, but failed in the context of my program wi= th 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:<br><br>"Strin= gs 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" target=3D"_b= lank">https://bugzilla.redhat.com/<wbr>show_bug.cgi?id=3D1048324</a><br><br= ></div><div>which helped me realize that the XML I was retrieving from Worl= dCat 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::encode= ($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 Bu= t this helps me move forward.<br><br></div><div>Thanks again! --Andy<div><d= iv class=3D"h5"><br><div><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 d= ir=3D"ltr"><<a href=3D"mailto:[email protected]" target=3D"_blank">ak= [email protected]</a>></span> wrote:<br><blockquote class=3D"gmail_quot= e" style=3D"margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204)= ;padding-left:1ex"><div 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 binary 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 alrea= dy got the XML stored in a variable, retrieved via LWP::Simple->get().<b= r><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'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 MARC::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></div></di= v> </blockquote></div><br></div> --0000000000006137a8056a2cd9ab--