2

我正在尝试使用 simplepie 获取提要中“id”标签的属性。

这是来自提要的代码片段:

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

我想从id标签中包含的im:id属性中获取数字 (488627)

我怎样才能得到这个?

我试过$item->get_item_tags('','im:id')但没有用

4

2 回答 2

2

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

于 2012-03-12T19:30:47.207 回答
0

SimplePie 要求命名空间的原因是因为它内部存储了给定命名空间下的节点元素。如果您不知道您的特定命名空间是什么,请使用 print_r 转储它:

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

如果您知道命名空间,您也可以直接访问子元素,或者编写一个简单的搜索器函数来逐步遍历每个命名空间并查找匹配的标签。

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

get_item_tags() 函数很愚蠢,通常不会做你想做的事,但它也很简单,很容易用你自己的特殊用途函数替换。原始来源是:

public function get_item_tags($namespace, $tag)
{
    if (isset($this->data['child'][$namespace][$tag]))
    {
        return $this->data['child'][$namespace][$tag];
    }
    else
    {
        return null;
    }
}
于 2014-02-01T09:51:50.360 回答