methuselah
methuselah

Reputation: 13206

Parsing YouTube video information from XML

Given the following XML output: https://www.youtube.com/feeds/videos.xml?channel_id=UCBJycsmduvYEL83R_U4JriQ, how do I iterate through all the entry nodes to get the yt:videoId value... for example:

<entry>
    <id>yt:video:Xyt9rmsxwDU</id>
    <yt:videoId>Xyt9rmsxwDU</yt:videoId>
    <yt:channelId>UCBJycsmduvYEL83R_U4JriQ</yt:channelId>
    <title>I've Made 1000 Videos?! Ask MKBHD V26!</title>
    <link rel="alternate" href="https://www.youtube.com/watch?v=Xyt9rmsxwDU"/>
    <author>
        <name>Marques Brownlee</name>
        <uri>https://www.youtube.com/channel/UCBJycsmduvYEL83R_U4JriQ</uri>
    </author>
    <published>2018-03-29T21:11:23+00:00</published>
    <updated>2018-04-02T14:24:16+00:00</updated>
    <media:group>
        <media:title>I've Made 1000 Videos?! Ask MKBHD V26!</media:title>
        <media:content url="https://www.youtube.com/v/Xyt9rmsxwDU?version=3" type="application/x-shockwave-flash" width="640" height="390"/>
        <media:thumbnail url="https://i1.ytimg.com/vi/Xyt9rmsxwDU/hqdefault.jpg" width="480" height="360"/>
        <media:description>
        1K in 4K. JK not all 4K. Still cool tho. The subreddit: http://reddit.com/r/MKBHD The iPhone X Unboxing: https://youtu.be/l0DoQYGZt8M GoPro dog video: https://youtu.be/YyrUBJQmUGs MKBHD Merch: http://shop.MKBHD.com Video Gear I use: http://kit.com/MKBHD/video-gear#recommendation17959 Tech I'm using right now: https://www.amazon.com/shop/influencer-0bfe542e ~ http://twitter.com/MKBHD http://snapchat.com/add/MKBHD http://google.com/+MarquesBrownlee http://instagram.com/MKBHD http://facebook.com/MKBHD
        </media:description>
        <media:community>
            <media:starRating count="43962" average="4.96" min="1" max="5"/>
            <media:statistics views="717951"/>
        </media:community>
    </media:group>
</entry>

This is my code so far, I can't figure out the next step:

function getChannelVideoInformation(youtubeApiKey, youtubeChannelId) {
  var queryUrl = envService.read('crossOrigin') + "https://www.youtube.com/feeds/videos.xml?channel_id=" + youtubeChannelId;
  return ($http.get(queryUrl).then(function (response) {
    var parser = new DOMParser();
    var xmlDoc = parser.parseFromString(response.data, "text/xml");
    // parse xmlDoc here and assign values to array
  }, handleError));
}

Upvotes: 0

Views: 284

Answers (1)

Cody Geisler
Cody Geisler

Reputation: 8617

Use something like

var nodes = doc.getElementsByTagName("yt:videoId");
var videoIds = [];
for(var i=0;i<nodes.length;i++){
  videoIds.push(nodes.item(i).innerHTML);
}

If you want to iterate through <entry>'s child nodes:

var nodes = doc.getElementsByTagName("entry").item(0).children
for(...){
  ...
}

Upvotes: 1

Related Questions