2012-02-26 30 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