2012-02-26 29 views
2

我想使用SimpleXML解析.opf-file(这是一个包含电子书文件的元数据文件 - .epubs在我的情况下)的内容。使用SimpleXML解析.opf文件

但是,我没有成功获取像<dc:title><dc:creator opf:file-as="xxx" opf:role="xxx">这样的项目。

对于文件hitchhiker.opf(见下文),我试过如下:

<?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; 
?> 

但是,这并不产生输出。

如果使用SimpleXML以外的其他工具可以更容易地完成这项工作,那也可以。

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> 

回答

6

你忘了元数据元素,例如

$package = simplexml_load_file('hitchhiker.opf'); 
echo $package->metadata->children('dc', true)->title; 

输出:漫游指南银河

+0

吧!我不知道为什么这个答案是downvoted ...这是绝对正确的。 – speendo 2012-02-26 12:54:51

+1

@speendo可能表明我不应该回答你的问题,因为答案太明显了,例如有人觉得我很难过。我真的不在乎你是否删除你的问题或接受我的答案。尽你所能去做。 – Gordon 2012-02-26 13:01:42