XML parsing help with DOMDocument

I am trying to parse the youtube playlist field.

URL: http://gdata.youtube.com/feeds/api/playlists/664AA68C6E6BA19B?v=2

I need: title, video ID and default thumbnail.

I can easily get the title, but I'm a little lost when it comes to nested elements

        $data = new DOMDocument();
        if($data->load("http://gdata.youtube.com/feeds/api/playlists/664AA68C6E6BA19B?v=2"))
        {       
            foreach ($data->getElementsByTagName('entry') as $video)
            {
                $title = $video->getElementsByTagName('title')->item(0)->nodeValue;
                $id    = ??
                $thumb = ??                 
            }
        }

Here is the XML (I removed the elements that are not suitable for this example)

<entry gd:etag="W/&quot;AkYGSXc9cSp7ImA9Wx9VGEk.&quot;">    
    <title>A GoPro Weekend On The Ice</title>

    <media:group>
        <media:thumbnail url="http://i.ytimg.com/vi/yk6wkfVNFQE/default.jpg" height="90" width="120" time="00:02:07" yt:name="default" />          
        <yt:videoid>yk6wkfVNFQE</yt:videoid>
    </media:group>

</entry>

I need the "videoid" and "url" from the default thumbnail

Thank!

+5
source share
1 answer

getElementsByTagName(), , ( namespace:element-name) getElementsByTagNameNS().

( ) , , , ( getAttribute()).

$yt    = 'http://gdata.youtube.com/schemas/2007';
$media = 'http://search.yahoo.com/mrss/';

// Inside your loop
$id    = $video->getElementsByTagNameNS($yt, 'videoid')->item(0)->nodeValue;
$thumb = $video->getElementsByTagNameNS($media, 'thumbnail')->item(0)->getAttribute('url');

, spring -board, XML-.

+7

All Articles