RSS XML 解析问题(如何从 RSS 提要获取媒体内容值?) [英] RSS XML Parsing issue (How to get media content value from the RSS feed?)

查看:41
本文介绍了RSS XML 解析问题(如何从 RSS 提要获取媒体内容值?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 rss feed url,它生成一个 xml,如下所示:

I have a rss feed url which generates an xml as follows:

 <?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:media="http://search.yahoo.com/mrss/" 
 xmlns:flow="http://www.flownetworks.com/schemas/media/0.1.0"><channel>
 <title>flow-Media Catalog</title>
 <link>http://catalog.flownetworks.com/catalogs/1/videos.mrss</link>
 <description>Video Catalog</description>
 <image>
    <url>http://images.flow-media.com/flow_media_current.png</url>
    <title>Get to know flow-Media</title>
    <link>http://www.flow-media.com</link>
 </image>
 <generator>flow-Media</generator>
 <item>
   <title>..</title>
   <link>..</link>
   <description>..</description>
   <pubDate>Wed, 01 May 2013 07:01:08 GMT</pubDate>
   <guid isPermaLink="false">9809880</guid>
   <flow:short_description/>
   <flow:video_product_id>52985890</flow:video_product_id>
   <flow:availability end="" start="2013-05-01T06:44:41Z"/>
   <flow:show/>
   <media:group>
   <media:category>US</media:category>
   <media:thumbnail url="http://images.flow-media.com/ap/2013/05/01/09d462107646ab09def454a1a923a423edd6d2d9_preview.jpg" height="360" width="640"/>
   <media:thumbnail url="http://images.flow-media.com/ap/2013/05/01/09d462107646ab09def454a1a923a423edd6d2d9_thumbnail.jpg" height="90" width="160"/>
   <media:content duration="101" medium="video" isDefault="true" url="http://player.flownetworks.com/swf/cube.swf?a=V5299690&amp;m=9&amp;w=420&amp;h=375" type="application/x-shockwave-flash" expression="full" height="375" lang="en-us" width="420">
    </media:content>
    </media:group>
   </item>
   </channel>

我想在数据库中保存图像和类别值.这是我的 php 代码:

I want to save image and category value in database.Here is my php code:

$apiURL="http://catalog.flownetworks.com/catalogs/01/videos/search.mrss?api_key=%206784cb3bed8f80c55054ac0de996f8e9f0bf8763";
$videoId="&video_product_id=".$videoID."";
$combinevURL=$apiURL.$videoId;
$mediafile = simplexml_load_file($combinevURL); 

问题:问题是simplexml_load_file"没有生成xml的类别名称和媒体缩略图值.我想从这个xml中获取值.请帮忙.

Problem: the problem is "simplexml_load_file" do not generate category name and media thumbnail image value of the xml.I want to get value from this xml.Please help.

推荐答案

要成功获取这些媒体元素,首先需要找到父元素.基本的 Simplexml 用法示例,我给你留了这段高度冗余的代码.

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.

因此,在将父级获取到变量中后 - 这次我们将其称为 $item - 它的工作原理如 已经暗示的重复问答材料,这里仅针对您的示例 XML:

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

通过使用命名空间前缀,它对应于突出显示的前缀部分.这需要使用 true 作为第二个参数.

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

您也可以使用替代名称空间 URI,这样您就可以省去第二个参数(默认为 FALSE):

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

无论您喜欢哪种变体,准确地告诉 simplexml 您关心 media 中的 children XML 命名空间使您能够访问该媒体组中的各个部分如你所知:

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

我希望这对您有所帮助,并明确地向您展示了它是如何用于命名空间元素的.您需要使用 SimpleXMLElement::children() 方法并指定哪个您的意思是从中获取子元素的命名空间.

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.

这篇关于RSS XML 解析问题(如何从 RSS 提要获取媒体内容值?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆