繁体   English   中英

用命名空间解析php中的RSS / XML

[英]Parsing RSS/XML in php with namespaces

我有一些看起来像这样的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,title和description之类的所有标签,但没有media:categorymedia:groupsomething:anything出现-它们只是被剥离了。

我如何解析此提要而不丢失它们?

您需要找到命名空间的定义位置,并找到命名空间映射到的字符串。 因此,例如,如果media名称空间映射到http://example.com/something

echo (string)$feed->children('http://example.com/something')->copyright;

输出:

大公司

使用SimpleXML的print_r()的结果并不总是为您提供完整的结构,但是元素在那里。

要获取嵌套元素,请尝试以下操作:

foreach($feed->children('http://example.com/something')->group->children('http://example.com/something')->content as $content)
{
    echo (string)$content->attributes()->bitrate;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM