chubbyk
chubbyk

Reputation: 6302

Get attributes from item(tag) using SimplePie

I'm trying to get attributes for "id" tag in feed with usage of simplepie.

This is the fragment of code from feed:

<updated>2012-03-12T08:26:29-07:00</updated>
<id im:id="488627" im:bundleId="dmtmobile">http://www.example.com</id>
<title>Draw Something by OMGPOP - OMGPOP</title>

I want to get number (488627) from im:id attribute contained in id tag

How can I get this ?

I tried $item->get_item_tags('','im:id') but it didn't work

Upvotes: 2

Views: 2264

Answers (2)

Doug
Doug

Reputation: 35136

The reason SimplePie asks for a namespace is because it internally stores the node elements under the given namespace. If you don't know what your specific namespace is, use print_r to dump it:

print_r($item->data['child']);

You can also directly access the child elements if you know the namespace, or write a simple seeker function to step through each namespace and look for a matching tag.

$data = $item->data['child']['im']['bundleId'][0]['data'];

The get_item_tags() function is stupid and doesn't usually do what you want, but it's also very simple and easy to replace with your own special purpose functions. Original source is:

public function get_item_tags($namespace, $tag)
{
    if (isset($this->data['child'][$namespace][$tag]))
    {
        return $this->data['child'][$namespace][$tag];
    }
    else
    {
        return null;
    }
}

Upvotes: 0

Ryan McCue
Ryan McCue

Reputation: 1658

If this is in an Atom 1.0 feed, you'll want to use the Atom namespace:

$data = $item->get_item_tags(SIMPLEPIE_NAMESPACE_ATOM_10,'id');

From there, you should then find that the attributes you want are:

$id = $data['attribs'][IM_NAMESPACE]['id']
$bundleID = $data['attribs'][IM_NAMESPACE]['bundleId']`

where IM_NAMESPACE is set to the im XML namespace (i.e. what the value of xmlns:im is).

Upvotes: 2

Related Questions