RSS XML Parsing issue (How to get media content value from the RSS feed?) [duplicate]

不想你离开。 提交于 2019-12-06 22:32:53
hakre

To successfully obtain these media elements you first of all need to find the parent element. How to access those non-namespaced elements is outlined in depth in the basic Simplexml usage examples, I spare you this highly redundant code here.

So after obtaining the parent into a variable - let's call it $item this time - it works as outlined in the already hinted duplicated Q&A material, just here specific to your example XML:

$media = $item->children('media', true);
                            |
                          __|__
 <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" 
                    xmlns:flow="http://www.flownetworks.com/schemas/media/0.1.0">

By using the namespace-prefix, it corresponds to the highlighted prefix part. This requires to use true as second parameter.

You can also use the alternative, the namespace-URI, so you can spare the second parameter (defaults to FALSE):

$media = $item->children('http://search.yahoo.com/mrss/');
                                        |
                                 _______|_____________________
 <rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" 
                    xmlns:flow="http://www.flownetworks.com/schemas/media/0.1.0">

Regardless which variant you prefer, telling simplexml exactly that you are concerned about children in the media XML-namespace enables you to access the various parts from within that media group as you know it:

$group = $media->group;
echo $group->category, "\n"; # US

I hope this is helpful and explicitly shows you how it works for the namespaced elements. You need to use the SimpleXMLElement::children() method and specify which namespace you mean of getting the children elements from.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!