2011-07-28 58 views
0

這與我發佈的問題類似,但我已經擴大了,因爲這已經可以解決。使用SimpleXML訪問XML中的名稱空間冒號節點PHP

我正在嘗試從此rss提要訪問評級和viewCount。現在,這些命名空間與yt:和gd: - 不同,但命名空間引用是來自YouTube的Feed的404,因此它似乎失敗了。請提供任何幫助嗎?

我想使用: -

$ytFeed->children('http://gdata.youtube.com/schemas/2007')->statistics->attributes('viewCount')); 

但它失敗......它不喜歡的工作,通常,比如我可以訪問媒體:與

$ytFeed->children('http://search.yahoo.com/mrss/')->group->category); 

這是原來的RSS,請看這些節點的底部: -

<?xml version='1.0' encoding='UTF-8'?> 
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'> 
    <id>http://gdata.youtube.com/feeds/api/videos/tDJFDsZFw2E</id> 
    <published>2009-03-02T07:02:49.000Z</published> 
    <updated>2011-07-19T06:57:29.000Z</updated> 
    <category scheme='http://schemas.google.com/g/2005#kind' term='http://gdata.youtube.com/schemas/2007#video' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/categories.cat' term='Music' label='Music' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='Other Lives' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='Jesse Tabish' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='Jenny Hsu' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='Jupiter Hotel' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='Portland' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='music' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='band' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='interview' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='mf magazine' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='fashion' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='hotel room' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='doug fir lounge' /> 
    <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' term='erik schultz' /> 
    <title type='text'>Other Lives</title> 
    <content type='text'>We sat down with Jesse Tabish and Jenny Hsu from Other Lives at the Jupiter Hotel in Portland, OR. We love these guys. Check them out at myspace.com/otherlivesVisit mf magazine at musicfashionmagazine.com or myspace.com/musicfashion</content> 
    <link rel='alternate' type='text/html' href='http://www.youtube.com/watch?v=tDJFDsZFw2E&amp;feature=youtube_gdata' /> 
    <link rel='http://gdata.youtube.com/schemas/2007#video.responses' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/tDJFDsZFw2E/responses' /> 
    <link rel='http://gdata.youtube.com/schemas/2007#video.related' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/tDJFDsZFw2E/related' /> 
    <link rel='self' type='application/atom+xml' href='http://gdata.youtube.com/feeds/api/videos/tDJFDsZFw2E' /> 
    <author> 
     <name>mfmagazine</name> 
     <uri>http://gdata.youtube.com/feeds/api/users/mfmagazine</uri> 
     </author> 
    <gd:comments> 
     <gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/tDJFDsZFw2E/comments' countHint='16' /> 
     </gd:comments> 
    <media:group> 
     <media:category label='Music' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>Music</media:category> 
     <media:content url='http://www.youtube.com/v/tDJFDsZFw2E?f=videos&amp;app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='600' yt:format='5' /> 
     <media:content url='rtsp://v3.cache3.c.youtube.com/CiILENy73wIaGQlhw0XGDkUytBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='600' yt:format='1' /> 
     <media:content url='rtsp://v6.cache3.c.youtube.com/CiILENy73wIaGQlhw0XGDkUytBMYESARFEgGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='600' yt:format='6' /> 
     <media:description type='plain'>We sat down with Jesse Tabish and Jenny Hsu from Other Lives at the Jupiter Hotel in Portland, OR. We love these guys. Check them out at myspace.com/otherlivesVisit mf magazine at musicfashionmagazine.com or myspace.com/musicfashion</media:description> 
     <media:keywords>Other Lives, Jesse Tabish, Jenny Hsu, Jupiter Hotel, Portland, music, band, interview, mf magazine, fashion, hotel room, doug fir lounge, erik schultz</media:keywords> 
     <media:player url='http://www.youtube.com/watch?v=tDJFDsZFw2E&amp;feature=youtube_gdata_player' /> 
     <media:thumbnail url='http://i.ytimg.com/vi/tDJFDsZFw2E/0.jpg' height='240' width='320' time='00:05:00' /> 
     <media:thumbnail url='http://i.ytimg.com/vi/tDJFDsZFw2E/1.jpg' height='90' width='120' time='00:02:30' /> 
     <media:thumbnail url='http://i.ytimg.com/vi/tDJFDsZFw2E/2.jpg' height='90' width='120' time='00:05:00' /> 
     <media:thumbnail url='http://i.ytimg.com/vi/tDJFDsZFw2E/3.jpg' height='90' width='120' time='00:07:30' /> 
     <media:title type='plain'>Other Lives</media:title> 
     <yt:duration seconds='600' /> 
     </media:group> 
    <gd:rating average='5.0' max='5' min='1' numRaters='17' rel='http://schemas.google.com/g/2005#overall' /> 
    <yt:statistics favoriteCount='10' viewCount='3572' /> 
    </entry> 
+0

可能的重複[404在YouTube XML架構 - 對於愛](http://stackoverflow.com/questions/6857532/404-on-youtube-xml-schemas-for-the-love-of) – Quentin

+0

我擴大了,並把它刪除。我一開始就這樣說過。 – waxical

+0

如果有人遇到這種情況,任何答案都不會涉及一個重要的問題:名稱空間的URI不需要以任何方式檢索*。它不是對XSD或其他定義的引用,只是使用域的所有權來確保沒有任何重複的任意標識符。 – IMSoP

回答

2

使用DomDocumentDomXpath

error_reporting(E_ALL^E_STRICT); 
ini_set('display_errors', 'on'); 

$dom = new DomDocument; 
$dom->load('data.xml'); 

$xpath = new DomXpath($dom); 
$xpath->registerNamespace('atom', 'http://www.w3.org/2005/Atom'); 
$xpath->registerNamespace('media', 'http://search.yahoo.com/mrss/'); 
$xpath->registerNamespace('gd', 'http://schemas.google.com/g/2005'); 
$xpath->registerNamespace('yt', 'http://gdata.youtube.com/schemas/2007'); 

$favoriteCountAttr = $xpath->query('/atom:entry/yt:statistics/@favoriteCount')->item(0); 
if ($favoriteCountAttr instanceof DomAttr) { 
    echo $favoriteCountAttr->nodeValue; // output: 10 
} 
+0

這是一個在屁股疼痛,但它確實工作,所以已被採納。謝謝! – waxical

4

在調用子函數時簡單傳遞名稱空間參數

$nodes = $xml->children('gd', true); 

參見手冊:http://www.php.net/manual/en/simplexmlelement.children.php

+0

簡單。但是,對於我以上的數據不適用。我有RTFM,但這是一個一致的問題,沒有解決。 - 我得到的「SimpleXMLElement :: children()最多隻有1個參數,2給出了」 – waxical

+1

@WiseDonkey「,正如您在手冊頁上所看到的,is_prefix僅添加了PHP 5.2,因此這意味着您的PHP版本比這更早。強烈建議您更新您使用的任何PHP版本,因爲不再支持任何版本<5.3。 – Gordon

+0

對,感謝指針 - 真的不可能更新atm。沒有其他辦法嗎? – waxical

5

好了,第一個問題是,你是濫用attributes方法:

$simpleXML->statistics->attributes('viewCount')); 

上面(從你的例子中解釋)你傳入你想要的屬性名稱,這不是attributes接受的參數。相反,上述內容將返回statistics元素的所有屬性,其中屬性本身的名稱空間爲viewCount,因爲該方法需要第一個參數(如果設置了)作爲屬性的名稱空間。

,如果你不是訪問的屬性作爲元素的數組,像這樣您的代碼將工作:

$ytFeed->children('http://gdata.youtube.com/schemas/2007')->statistics['viewCount']; 

如果你想避免每次使用完整的命名空間URI和children方法,你可以通過使用getNamespaces方法映射所有名稱空間來一個陣列,並且然後經由children方法映射與該命名空間孩子一個對象,如簡化:

$namespaces = $ytFeed->getNameSpaces(true); 
$yt = $ytFeed->children($namespaces['yt']); 
$yt->statistics['viewCount']; 

// Access all media: namespaced group elements like in your "working" example: 
$media = $ytFeed->children($namespaces['media']); 
$media->group->category; 

我意識到這是在2葉一個答案被接受了,但接受的答案實際上只是一個更好的文檔和更直觀的選擇,但並沒有告訴其他人發現這個問題的實際方法來實現SimpleXML,最終只需要一個額外的兩行代碼添加到DOMDocument的6到8個額外的行和一個不同的擴展中,這可能會誤導未來的讀者認爲它不能用SimpleXML完成。它可以,而且很容易,不是很明顯。

+0

哇,我想這個應該是被接受的答案。 – freakyroach

+0

'getNamespaces'技巧不再是必需的,除非你是一個真正古老的PHP版本,因爲'children()'和'attributes()'現在都可以通過給出第二個參數'true '。依靠前綴(無論哪種方式)的確意味着所用前綴的改變(不會改變代碼的「含義」,儘管如此,更好的方法是用你自己的名字爲你認識的命名空間構建一個數組將會使用(它的URI部分就像'http://gdata.youtube.com/schemas/2007',這是保證不會改變的)。 – IMSoP

相關問題