2012-04-07 84 views
1

我有以下rss格式,並且我無法彈出'content:encoded'值。如何從RSS獲取具有自定義名稱的元素?

<item> 
    <title>some title</title> 
    <link>some link</link> 
    <pubDate>Sat, 07 Apr 2012 5:07:00 -0700</pubDate> 
    <content:encoded><![CDATA[this value]]></content:encoded> 
</item> 

我寫了這個功能,一切正常,除了「內容:編碼」字段,讓我這個錯誤:「通知:試圖讓非對象的特性」

function rssReader($url) { 
    $doc = new DOMDocument(); 
    $doc->load($url); 
    $fields = array('title', 'description', 'link', 'pubDate', 'content:encoded'); 
    $nodes = array(); 

    foreach ($doc->getElementsByTagName('item') as $node) { 
    $item = array(); 
    var_export($node, true); 
    foreach ($fields as $field) 
     $item[$field] = $node->getElementsByTagName($field)->item(0)->nodeValue; 
    $nodes[] = $item; 
    } 

    return $nodes; 
} 
+0

告訴我們如何你試過...... – 2012-04-07 13:50:15

+0

我認爲你必須瞭解更多關於命名空間 – Michelle 2012-04-07 14:00:44

+0

您的命名空間是什麼意思?你能指導我嗎? – 2012-04-07 15:21:02

回答

2

您需要使用getElementsByTagNameNS而不是getElementsByTagName'content:encoded'標籤:

foreach ($fields as $field){ 
    if($field == 'content:encoded'){ 
     $item[$field] = $node->getElementsByTagNameNS('contentNamespaceURI','encoded')->item(0)->nodeValue; 
    }else{ 
     $item[$field] = $node->getElementsByTagName($field)->item(0)->nodeValue; 
    } 
} 

你可以找到rss'contentNamespaceURI'。必須有這樣的東西:

xmlns:content="contentNamespaceURI" 
1

這裏的標籤名稱是「編碼」。

只需使用

$content => $node->getElementsByTagName('encoded')->item(0)->nodeValue 
相關問題