开发者

Retrieve an XML psuedo class value in PHP?

I'm trying to develop a site with some youtube videos. After I retrieve the XML file from 开发者_JAVA百科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.


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;


Those XML elements are namespaced. You need to get the namespace information.

Example

// get nodes in media: namespace for media information
$media = $entry->children('http://search.yahoo.com/mrss/');
  
// get video player URL
$attrs = $media->group->player->attributes();


I'm assuming you're using SimpleXML here

$nsMedia = $xml->children('http://search.yahoo.com/mrss/');
$group = $nsMedia->group;
$nsYt = $group->children('http://gdata.youtube.com/schemas/2007');
$duration = $nsYt->duration;
echo $duration['seconds'];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜