Note that there are some explanatory texts on larger screens.

plurals
  1. POParsing annoying Youtube API XML feeds with jQuery
    primarykey
    data
    text
    <p>I am learning how to utilise external XML feeds within our Intranet, to better serve our staff and reduce their need to go outside the network for content.</p> <p>I can render the basic information from the feed without a problem. Where I come unstuck is trying to display one of the video thumbnails to use as part of the my chosen layout.</p> <p><strong>Problem</strong></p> <p>I am trying to grab the value of <code>URL</code> property from the first <code>media:thumbnail</code> element for use in an <code>img</code> tag, but whatever I try I can't seem to render the contents of it.</p> <p>Eg; <code>&lt;media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/default.jpg'... /&gt;</code></p> <p>The piece of text I'd want to extract &amp; use from the above example is <code>http://i.ytimg.com/vi/CQP_AuT4zXQ/default.jpg</code></p> <p>I suspect why I'm not able to parse the value of this element has something to do with the element name having a period (:) separator in it, and/or my developing knowledge of jQuery suspects I'm not calling or attempting to parse this element or part thereof correctly.</p> <p>I'd also like to render the value of <code>media:keywords</code> as well, which would be solved by understanding the same issue.</p> <p><strong>Code used to render the XML</strong></p> <pre><code>&lt;script type="text/javascript"&gt; /* learned from http://www.switchonthecode.com/tutorials/xml-parsing-with-jquery */ /* this can be used to parse any file in an XML format */ $(document).ready(function() { /* perform an asynchronous ajax request using the relevant variables Documentation: http://api.jquery.com/jQuery.ajax/ */ $.ajax({ type: "GET", /* define url of xml file to parse */ url: "/pr/rss_repository/youtube.xml", /* assign it a data type */ dataType: "xml", /* state name of function to run if file is successfully parsed */ success: parseXml }); }); function parseXml(xml) /* this is where the xml file is parsed and converted into a HTML output */ { //for each item node in the xml file $(xml).find("item").each(function() { //print the following html, inserting the relevant data within the nodes of item //this is the heading $("#output").append("&lt;h2&gt;&lt;a href=\"" + $(this).find("link").text() + "\" target=\"_blank\"&gt; " + $(this).find("title").text() + "&lt;/a&gt;&lt;/h2&gt;\n"); //this is a new paragraph with the publication date $("#output").append("&lt;p&gt;" + $(this).find("pubDate").text() + "&lt;/p&gt;\n"); //this is the description text of the item $("#output").append("&lt;p&gt;" + $(this).find("description").text() + "&lt;/p&gt;\n"); $("#output").append("&lt;p&gt;&lt;img src=\"" + $(this).find("media:thumbnail").text() + "\"&gt;&lt;/p&gt;\n"); }); //end for each //end function } &lt;/script&gt; </code></pre> <p><strong>Resultant HTML</strong></p> <pre><code>&lt;h2&gt;&lt;a href="http://www.youtube.com/watch?v=CQP_AuT4zXQ&amp;amp;feature=youtube_gdata" target="_blank"&gt; IFA World Forestry Day 2010 forum - Bob Pearce, FIFWA (Part 1)&lt;/a&gt;&lt;/h2&gt; &lt;p&gt;Thu, 01 Apr 2010 06:44:19 +0000&lt;/p&gt; &lt;p&gt;Bob Pearce, Executive Director - Forest Industries Federation of Western Australia delivers a presentation on the impact of timber harvesting in healthy forests at the Institute of Foresters of Australia 2010 World Forestry Day forum.&lt;/p&gt; &lt;p&gt;&lt;img src=""&gt;&lt;/p&gt; ... </code></pre> <p><strong>Sample of XML from Youtube being parsed</strong></p> <p>The file is grabbed and brought into our servers here, to save the file being downloaded each time it is called. We pull it into the network using VBScript which dumps the contents returned from the URL queried.</p> <p>Example of data, from <a href="http://gdata.youtube.com/feeds/api/videos?alt=rss&amp;author=fpcwa" rel="nofollow noreferrer">http://gdata.youtube.com/feeds/api/videos?alt=rss&amp;author=fpcwa</a>:</p> <pre><code>&lt;?xml version='1.0' encoding='UTF-8'?&gt; &lt;rss xmlns:atom='http://www.w3.org/2005/Atom' xmlns:media='http://search.yahoo.com/mrss/' xmlns:openSearch='http://a9.com/-/spec/opensearch/1.1/' xmlns:gd='http://schemas.google.com/g/2005' xmlns:gml='http://www.opengis.net/gml' xmlns:yt='http://gdata.youtube.com/schemas/2007' xmlns:georss='http://www.georss.org/georss' version='2.0'&gt; &lt;channel&gt; ... standard channel description &lt;item&gt; &lt;guid isPermaLink='false'&gt;tag:youtube.com,2008:video:CQP_AuT4zXQ&lt;/guid&gt; &lt;pubDate&gt;Thu, 01 Apr 2010 06:44:19 +0000&lt;/pubDate&gt; &lt;atom:updated&gt;2010-04-29T00:03:31.000Z&lt;/atom:updated&gt; &lt;category domain='http://schemas.google.com/g/2005#kind'&gt;http://gdata.youtube.com/schemas/2007#video&lt;/category&gt; &lt;category domain='http://gdata.youtube.com/schemas/2007/categories.cat'&gt;News&lt;/category&gt; &lt;category domain='http://gdata.youtube.com/schemas/2007/keywords.cat'&gt;ifa&lt;/category&gt; ... remaining categories &lt;title&gt;IFA World Forestry Day 2010 forum - Bob Pearce, FIFWA (Part 1)&lt;/title&gt; &lt;link&gt;http://www.youtube.com/watch?v=CQP_AuT4zXQ&amp;feature=youtube_gdata&lt;/link&gt; &lt;author&gt;fpcwa&lt;/author&gt; &lt;yt:accessControl action='comment' permission='allowed'/&gt; ... &lt;gd:comments&gt; &lt;gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/CQP_AuT4zXQ/comments?client=ytapi-google-jsdemo' countHint='0'/&gt; &lt;/gd:comments&gt; &lt;media:group&gt; &lt;media:category label='News &amp; Politics' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'&gt;News&lt;/media:category&gt; &lt;media:content url='http://www.youtube.com/v/CQP_AuT4zXQ?f=videos&amp;c=ytapi-google-jsdemo&amp;app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='587' yt:format='5'/&gt; &lt;media:content url='rtsp://v1.cache6.c.youtube.com/CjcLENy73wIaLgl0zfjkAv8DCRMYDSANFEITeXRhcGktZ29vZ2xlLWpzZGVtb0gGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='587' yt:format='1'/&gt; &lt;media:content url='rtsp://v2.cache3.c.youtube.com/CjcLENy73wIaLgl0zfjkAv8DCRMYESARFEITeXRhcGktZ29vZ2xlLWpzZGVtb0gGUgZ2aWRlb3MM/0/0/0/video.3gp' type='video/3gpp' medium='video' expression='full' duration='587' yt:format='6'/&gt; &lt;media:credit role='uploader' scheme='urn:youtube'&gt;fpcwa&lt;/media:credit&gt; &lt;media:description type='plain'&gt;Bob Pearce, Executive Director - Forest Industries Federation of Western Australia delivers a presentation on the impact of timber harvesting in healthy forests at the Institute of Foresters of Australia 2010 World Forestry Day forum.&lt;/media:description&gt; &lt;media:keywords&gt;ifa, institute of foresters, australia, forestry, world forestry day, 2010, forum, healthy forests, forest, forests, bob pearce, fifwa&lt;/media:keywords&gt; &lt;media:player url='http://www.youtube.com/watch?v=CQP_AuT4zXQ&amp;feature=youtube_gdata'/&gt; &lt;media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/default.jpg' height='90' width='120' time='00:04:53.500'/&gt; &lt;media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/2.jpg' height='90' width='120' time='00:04:53.500'/&gt; &lt;media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/1.jpg' height='90' width='120' time='00:02:26.750'/&gt; &lt;media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/3.jpg' height='90' width='120' time='00:07:20.250'/&gt; &lt;media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/hqdefault.jpg' height='360' width='480'/&gt; &lt;media:title type='plain'&gt;IFA World Forestry Day 2010 forum - Bob Pearce, FIFWA (Part 1)&lt;/media:title&gt; &lt;yt:aspectRatio&gt;widescreen&lt;/yt:aspectRatio&gt; &lt;yt:duration seconds='587'/&gt; &lt;yt:uploaded&gt;2010-04-01T06:44:19.000Z&lt;/yt:uploaded&gt; &lt;yt:videoid&gt;CQP_AuT4zXQ&lt;/yt:videoid&gt; &lt;/media:group&gt; &lt;yt:statistics favoriteCount='0' viewCount='10'/&gt; &lt;/item&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload