2012-02-26 3 views
2

SimpleXML을 사용하여 .opf-file (필자의 경우 ebook-files - .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

오른쪽 히치하이커를위한 안내서! 나는 왜이 대답이 왜곡되었는지 모르겠다 ... 그것은 절대적으로 옳다. – speendo

+1

@speendo는 답변이 너무 명확하기 때문에 질문에 답변하지 말아야 함을 나타낼 수 있습니다. 누군가 내가 repwhoring하고 있다고 느꼈다. 나는 당신이 당신의 질문을 삭제하거나 내 대답을 수락하는지에 대해 솔직히 걱정하지 않습니다. 당신이 적합하다고 생각하는대로하십시오. – Gordon

관련 문제