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

用名称空间解析php中的RSS/XML,php,xml,parsing,rss,Php,Xml,Parsing,Rss,我有一些RSS看起来像这样: <item> <guid isPermaLink="false">2284767032</guid> <title>title goes here...</title> <description> Description </description> <author>author name</author> <dcterms:valid>start

我有一些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>

2284767032
标题在这里。。。
描述
作者姓名
开始=2012-09-28T17:06:00Z;scheme=W3C-DTF
第一类
第二类
第三类
大公司
一些关键词;
a234dfasf4f
http://url.jpg
我使用以下代码来解析它:

  $feed = simplexml_load_file('http://feedurl.com');
  echo "<pre>";
  print_r($feed);
  echo "</pre>";
$feed=simplexml\u load\u文件('http://feedurl.com');
回声“;
打印(feed);
回声“;
问题是我得到了所有的标签,比如guid、title和description,但是没有一个
media:category
media:group
something:anything
显示出来-它们只是被去掉了


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

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

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

大公司

使用SimpleXML的
print\u r()
的结果并不总是提供完整的结构,但是元素就在那里

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


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

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

大公司

使用SimpleXML的
print\u r()
的结果并不总是提供完整的结构,但是元素就在那里

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


那会在RSS文档的标题中吗?@Wes是的,通常是这样,例如:
这是
itunes
命名空间的定义,定义为
http://www.itunes.com/dtds/podcast-1.0.dtd
。好的,在大对象/数组中是否有将它们全部合并到一起的方法?这是可行的,但对于嵌套的对象,我得到了以下信息:-我如何获得这些信息?您可以手动将数据添加到大数组中,但我不这么认为。要获得嵌套的,我认为您必须再次调用
children()
。请参阅我的编辑。这会在RSS文档的标题中吗?@Wes是的,通常是这样,例如:
这是
itunes
命名空间的定义,定义为
http://www.itunes.com/dtds/podcast-1.0.dtd
。好的,在大对象/数组中是否有将它们全部合并到一起的方法?这是可行的,但对于嵌套的对象,我得到了以下信息:-我如何获得这些信息?您可以手动将数据添加到大数组中,但我不这么认为。要获得嵌套的,我认为您必须再次调用
children()
。请参阅我的编辑。