使用Simple XML读取XML属性

时间:2012-08-13 13:14:35

标签: php simplexml

我想从RSS提要中读出Simple XML的特定属性。

项目示例:

<item>
     <category>3500</category>
     <category>20</category>
     <category>17</category>
     <title>Blabla</title>
     <description>Blabla</description>
     <content:encoded>Blabla</content:encoded>
     <link>http://...</link>
     <guid isPermaLink="false">http://...</guid>
     <media:content url="http://image.jpg" medium="image">
              <media:title type="html">caption needed</media:title>
     </media:content>
     <pubDate>Mon, 13 Aug 2012 11:40:53 +0200</pubDate>
</item>

我想阅读媒体中的网址:内容。

要阅读我使用的其他代码:$ xml-&gt; channel-&gt; item [$ i] - &gt; title

要阅读我尝试过的媒体网址属性:$ xml-&gt; channel-&gt; item [$ i] - &gt; media-&gt; attributes() - &gt; url

但这只返回NULL。它与以下内容有关:内容是名称空间。

1 个答案:

答案 0 :(得分:1)

元素的名称实际上是content,而不是media(这只是一个任意前缀,应该与命名空间相关联,以构成格式良好的XML)。与encoded是元素名称而不是content相同。

$xml->item[0]->children('whatever-the-namespace-tied-to-media-is')->content->attributes()->url