2014-08-29 52 views
0

如何使用simplexml讀取iTunes RSS中的機箱url標記?simplexml閱讀enclousure html標記

FEED:

<?xml version="1.0" encoding="UTF-8"?> 
<rss xmlns:itunes="http://www.itunes.com/dtds/podcast-1.0.dtd" version="2.0"> 

<channel> 
<item> 
    <itunes:subtitle> subtitle </itunes:subtitle> 
    <enclosure url="http://www.myWe/mySound.mp3" length="624557" type="audio/mp3"/> 
</item> 
</channel> 
</rss> 

PHP:

foreach ($feed->channel->item as $item) { 
    $iTunes_item = $item->children('http://www.itunes.com/dtds/podcast-1.0.dtd'); 

    $item_subtitle = $item->subtitle->attributes(); 
    $item_url = $iTunes_item->enclosure->attributes('url'); // enclosure url 

    echo $item_subtitle; 
    echo $item_url; 
} 
+1

rss-> channel-> item-> enclosure-> attributes() - > url我認爲應該得到的網址。 – 2014-08-29 13:49:28

+0

@Matt:是的!這是正確的答案。謝謝 – Nrc 2014-08-29 13:58:04

+0

作爲回答加入 – 2014-08-29 13:59:50

回答

6

訪問URL使用;

rss->channel->item->enclosure->attributes()->url 

試試這個獲取小標題;

$namespaces = $item->getNameSpaces(true); 
$nodes = $item->children($namespaces['itunes']); 
echo $nodes->subtitle; 
+0

它令我驚訝,它沒有iTunes地址的作品。這可以應用於例如標籤嗎?:例如,如果沒有iTunes地址,這不起作用:$ feed-> channel-> item-> subtitle-> attributes(); – Nrc 2014-08-29 14:09:35

+0

你已經很好地回答了這個問題。但是,我更新了這個問題。你知道如何閱讀小標題嗎?似乎同樣不能應用? – Nrc 2014-08-29 14:21:48

+0

更新了答案@Nrc – 2014-08-29 14:26:43