I'd like to parse the content of an .opf-file (that's a file that contains the meta data of ebook-files - .epubs in my case) using SimpleXML.
However, I don't succeed getting items like <dc:title>
or <dc:creator opf:file-as="xxx" opf:role="xxx">
.
For the file hitchhiker.opf (see below) I tried the following:
<?php
$opf = simplexml_load_file("hitchhiker.opf");
$namespaces = $opf->getNameSpaces(true);
$dc = $opf->children($namespaces['dc']);
echo $dc->title;
echo "\r\n";
echo $dc->creator;
?>
but that doesn't produce an output.
If this can be done more easily with other tools than SimpleXML this would be fine too.
hitchhiker.opf:
<?xml version='1.0' encoding='utf-8'?>
<package xmlns="http://www.idpf.org/2007/opf" unique-identifier="uuid_id">
<metadata xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:opf="http://www.idpf.org/2007/opf">
<dc:identifier opf:scheme="calibre" id="calibre_id">8</dc:identifier>
<dc:identifier opf:scheme="uuid" id="uuid_id">36515eb7-1a25-4469-b55e-0042d45ca508</dc:identifier>
<dc:title>The Hitchhiker's Guide to the Galaxy</dc:title>
<dc:creator opf:file-as="Adams, Douglas" opf:role="aut">Douglas Adams</dc:creator>
<dc:contributor opf:file-as="calibre" opf:role="bkp">calibre (0.7.43) [http://calibre-ebook.com]</dc:contributor>
<dc:date>1997-06-22T07:00:00+00:00</dc:date>
<dc:description>SUMMARY:
Don't panic! You're not timetripping! It's the tenth anniversary of the publication of Douglas Adams's zany, best-selling novel, and to celebrate Harmony is reissuing a special edition of this cult classic!By now the story is legendary. Arthur Dent, mild-mannered, out-to-lunch earth-ling, is plucked from his planet by his friend Ford Prefect just seconds before it was demolished to make way for a hyper-space bypass. Ford, posing as an out-of-work actor, is a researcher for the revised edition of The Hitchhiker's Guide to the Galaxy. Together the gruesome twosome begin their now-famous inter-galactic journey through time, space and best-sellerdom.For Hitchhiker fanatics (you know who you are!) who've read the books, seen the television program, and listened to the radio show, as well as newcomers to Douglas Adams's unique universe -- remember -- don't panic, don't forget to bring a towel, and don't forget to celebrate The Hitchhiker's Guide to the Galaxy's tenth anniversary by wearing your bathrobe.From the Hardcover edition.</dc:description>
<dc:publisher>Random House, Inc.</dc:publisher>
<dc:identifier opf:scheme="ISBN">9780345418913</dc:identifier>
<dc:language>en</dc:language>
<dc:subject>Science-Fiction</dc:subject>
</metadata>
<guide>
<reference href="Hitchhiker's Guide to the Galaxy, The - Douglas Adams.jpg" type="cover" title="Cover"/>
</guide>
</package>
You forgot the metadata element, e.g.
$package = simplexml_load_file('hitchhiker.opf');
echo $package->metadata->children('dc', true)->title;
Output: The Hitchhiker's Guide to the Galaxy