2

我正在通过 simpleXML 加载一个 XML 文件。我充其量是 PHP 的新手和解析 PHP 的新手,但我有点困惑

我试图了解我存储的变量的结构,所以我尝试了 var_dump()

让我感到困惑的是我在 XML 中寻找的数据不在转储数据中的任何位置

http://gdata.youtube.com/feeds/api/videos?q=surfing&max-results=25

这是我目前使用的 URL。但是,例如,在转储的变量数据中找不到持续时间/秒——它在哪里以及如何访问它?

这是我得到的输出:http: //pastebin.com/ggumXTEu

4

1 回答 1

2

路径<yt:duration>/feed/entry/media:group/yt:duration/

使用 var_dump() 时看不到这些元素,因为它们属于命名空间(媒体和 yt)。

您可以使用 SimpleXMLElement->children() 来访问这些元素,您需要提供 namespace-uri 作为参数children()

<ol>
<?php
$xml=simplexml_load_file('http://gdata.youtube.com/feeds/api/videos?q=surfing&max-results=25');
foreach($xml->entry as $entry)
{
  $duration=$entry
              ->children('http://search.yahoo.com/mrss/')
                ->group
                  ->children('http://gdata.youtube.com/schemas/2007')
                    ->duration
                      ->attributes()
                        ->seconds;
       printf('<li><strong>%s</strong>( %ds)</li>',$entry->title,$duration); 
}
?> 
</ol>
于 2012-11-15T22:28:55.107 回答