doushu9253 2011-06-15 00:09
浏览 46
已采纳

在PHP中检索XML伪类值?

I'm trying to develop a site with some youtube videos. After I retrieve the XML file from their API, I have the following.

    <?xml version='1.0' encoding='UTF-8'?>
<entry xmlns='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:yt='http://gdata.youtube.com/schemas/2007'>
    <id>http://gdata.youtube.com/feeds/api/videos/4ZsiqqOyWx8</id>
    <published>2007-08-03T05:48:51.000Z</published>
    [...]
    <author>
        <name>ak326</name>
        <uri>http://gdata.youtube.com/feeds/api/users/ak326</uri>
    </author>
    <gd:comments>
        <gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/4ZsiqqOyWx8/comments' countHint='0'/>
    </gd:comments>
    <media:group>

        [...]
        <yt:duration seconds='222'/>
    </media:group>
    <gd:rating average='5.0' max='5' min='1' numRaters='4' rel='http://schemas.google.com/g/2005#overall'/>
    <yt:statistics favoriteCount='8' viewCount='2674'/>
</entry>

I'm trying to retrieve the length of this video from with PHP but with

echo $xml->media->yt

But it's not working. I think it has something to do with the psuedo class on media and yt but I don't know how to select those.

  • 写回答

3条回答 默认 最新

  • donglan8870 2011-06-15 03:50
    关注

    Try DOMXPath

    $xml = new DOMDocument();
    $xml->load(path/to/file);
    $xpath = new DOMXPath($xml);
    $xpath->registerNamespace("atom", "http://www.w3.org/2005/Atom");
    $xpath->registerNamespace("media", "http://search.yahoo.com/mrss/");
    $xpath->registerNamespace("yt", "http://gdata.youtube.com/schemas/2007");
    print $xpath->query("/atom:entry/media:group/yt:duration/@seconds")->item(0)->value;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集