【问题标题】:I cant display the second media:content with simpleXML我无法显示第二个媒体:使用 simpleXML 的内容
【发布时间】:2018-07-23 10:24:12
【问题描述】:

我似乎无法检索第二个媒体:内容,即上面带有width="460" 的那个。

<item>
   <title>...</title>
   <link>...</link>
   <description>...</description>

   <category domain="...">title</category>
   <category domain="...">Benjamin Netanyahu</category>
   <category domain="...">Judaism</category>
   <category domain="...">Religion</category>

   <pubDate>Mon, 23 Jul 2018 07:00:27 GMT</pubDate>

   <media:content width="140" url="**(i can retrieve this image)**">
      <media:credit scheme="urn:ebu">Photograph: Author 1</media:credit>
   </media:content>

   <media:content width="460" url="**(but i cant retrieve this one)**">
      <media:credit scheme="urn:ebu">Photograph: Author 2</media:credit>
   </media:content>

   <dc:creator>Touma-Sliman</dc:creator>
   <dc:date>2018-07-23T07:00:27Z</dc:date>
</item>

这是我当前的代码:

 foreach ($xml->channel->item as $key=>$item)
 {
    $results = (string)$item[0]->children('media', true)->content->attributes()['url'];
 }

【问题讨论】:

    标签: php simplexml


    【解决方案1】:

    ...-&gt;content-&gt;......-&gt;content[0]-&gt;... 的简写,因此您只需要请求该节点的第一个实例。

    相反,您需要以与循环-&gt;item 节点完全相同的方式循环-&gt;content 节点:

    foreach ( $item->children('media', true)->content as $content ) 
    

    或者如果出于某种原因你知道你明确想要第二个节点,你可以要求项目[1]

    $second_url = (string)$item->children('media', true)->content[1]->attributes()['url'];
    

    (我已将$item[0] 替换为$item,因为该变量在每次循环中都代表了&lt;item&gt; 的单个实例。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-04-29
      • 1970-01-01
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-29
      • 2016-09-20
      相关资源
      最近更新 更多