Yarin Gold
Yarin Gold

Reputation: 509

How to get elements with custom name from rss?

I have the following rss format, and i can't eject the 'content:encoded' value.

<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>

i wrote this function, everything works well except the 'content:encoded' field that give me this error: 'Notice: Trying to get property of non-object'

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;
}

Upvotes: 1

Views: 1583

Answers (2)

Arindam Mani Das
Arindam Mani Das

Reputation: 608

The Tag name here is "encoded".

Just use

$content => $node->getElementsByTagName('encoded')->item(0)->nodeValue

Upvotes: 1

Engineer
Engineer

Reputation: 48793

You need to use getElementsByTagNameNS instead of getElementsByTagName for 'content:encoded' tag:

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;
  }
}

You could find 'contentNamespaceURI' in rss. There must be something like:

   xmlns:content="contentNamespaceURI"

Upvotes: 2

Related Questions