我有一些RSS是这样的:
<item>
<guid isPermaLink="false">2284767032</guid>
<title>title goes here...</title>
<description> Description </description>
<author>author name</author>
<dcterms:valid>start=2012-09-28T17:06:00Z;scheme=W3C-DTF</dcterms:valid>
<media:category scheme="" label="">cat1</media:category>
<media:category scheme="" label="">cat2</media:category>
<media:category scheme="" label="">cat3</media:category>
<media:copyright>Big Company</media:copyright>
<media:keywords>some;keywords;</media:keywords>
<media:group>
<media:content bitrate="643.386" medium="video" duration="72.144" expression="full" fileSize="5802051" framerate="29.97" type="video/x-flv" height="360" url="..." width="640"/>
<media:content bitrate="1242.571" medium="video" duration="72.144" expression="full" fileSize="11205501" framerate="29.97" type="video/x-flv" height="480" url="..." width="854"/>
</media:group>
<link>a234dfasf4f</link>
<plmedia:defaultThumbnailUrl>
http://url.jpg
</plmedia:defaultThumbnailUrl>
</item>
我使用以下代码来解析它:
$feed = simplexml_load_file('http://feedurl.com');
echo "<pre>";
print_r($feed);
echo "</pre>";
问题是,我得到所有的标签,如guid,标题和描述,但没有media:category
或media:group
或something:anything
显示-他们只是剥离。
我如何解析这个feed而不丢失它们?
您需要找到名称空间定义的位置,并找到名称空间映射到的字符串。例如,如果media
名称空间映射到http://example.com/something
:
echo (string)$feed->children('http://example.com/something')->copyright;
输出:
大公司
print_r()
与SimpleXML的结果并不总是给你完整的结构,但元素是存在的。
要获取嵌套元素,尝试如下操作:
foreach($feed->children('http://example.com/something')->group->children('http://example.com/something')->content as $content)
{
echo (string)$content->attributes()->bitrate;
}