Pulling a YouTube Playlist XML into a Query Result

Hey all, long time.

I’m trying to create a query out of a YouTube playlist xml feed and am getting lost in the weeds trying to loop on “entry”. Any quick suggestions?

Thanks. Mik

<cfset XMLfeed = XMLParse("https://www.youtube.com/feeds/videos.xml?playlist_id=PLqOw-sfqn2fI-x8gtPbk2jLHvMUoRlr0I",false)>
<cfdump var="#XMLfeed["feed"]["author"]["name"]#">
<hr>
<cfdump var="#XMLfeed["feed"]#">

I got some help from @bennadel with a question that I think is the same…,
In the very least it might steer you in the right direction.

This will loop through the ‘entry’ nodes:

<cfloop array="#XmlFeed.feed.xmlChildren#" index="node"> 
    <cfif node.XmlName == "entry">
        <cfdump var="#node#"> 
    </cfif>
</cfloop>

Thank you! One last quick one: some of the nodes have a name with a : colon in them, like “media:group”. I tried to pull the description and got an error. The code works fine without that second line (br). How would I get around that?

<cfloop array="#XmlFeed.feed.xmlChildren#" index="node">
  <cfif node.XmlName == "entry">
    <cfdump var="#node#" label="NODE" expand="0">
      <p>#node["title"].XmlText# - #node["link"].XmlAttributes.href#
      <br>#node["media:group"].XmlChildren["media:description"].XmlText#</p>
  </cfif>
</cfloop>

XmlChildren is a node list - a sort of array, so you can’t use that the way you have tried to.

To access the media:description node you can use:

node["media:group"]["media:description"].XmlText
1 Like

Just thinking, you can also use XmlSearch to get your ‘entry’ nodes:

<cfset entries = XmlSearch(XMLFeed, "//:entry")>

The XPath //: says get all of the follwing nodes ignoring the namespace.

2 Likes

Booyah! You are awesome, Martin.

This is my interim code. I’m embedding it into my code in a more decorative kind of way, but this is a good start for anyone wishing to display a YouTube Playlist XML in ColdFusion (some good seo juice there). I hope it helps your project! -Mik

<cfset link = "https://www.youtube.com/playlist?list=PLqOw-sfqn2fI-x8gtPbk2jLHvMUoRlr0I">
<cfhttp url="#trim(replace(link,"playlist?list=","feeds/videos.xml?playlist_id=","all"))#" method="GET">
<cfset XMLfeed = XMLParse(cfhttp.fileContent,false)>

<!---
<cfdump var="#XMLfeed#">
--->
<cfoutput>
<h2>#XMLfeed["feed"]["title"].XmlText#</h2>
<p>#XMLfeed["feed"]["author"]["name"].XmlText#</p>

<cfloop array="#XmlFeed.feed.xmlChildren#" index="node">
  <cfif node.XmlName == "entry">
    <li>
      <p>
        <img src="#node["media:group"]["media:thumbnail"].XmlAttributes.url#" style="float:left; margin-right:10px; height:140px;">
        <strong>#node["title"].XmlText#</strong>
        <br><a href="#node["link"].XmlAttributes.href#" target="_blank">#node["link"].XmlAttributes.href#</a>
        <br><i>Published: #node["published"].XmlText#</i>
        <br><i>Updated: #node["updated"].XmlText#</i>
        <br><i>Description: #node["media:group"]["media:description"].XmlText#</i>
        <br><i>Views: #node["media:group"]["media:community"]["media:statistics"].XmlAttributes.views#</i>
      </p>
    </li>
  </cfif>
</cfloop>
</cfoutput>

This is my interim code. I’m embedding it into my code in a more decorative kind of way, but this is a good start for anyone wishing to display a YouTube Playlist XML in ColdFusion (some good seo juice there). I hope it helps your project! -Mik

[trying to post the code but keep getting an error]