我正在學習如何在Intranet中利用外部XML提要,以更好地服務我們的員工並減少他們在網絡之外訪問內容的需求。使用jQuery解析令人討厭的Youtube API XML提要
我可以從源提供基本信息而不會出現問題。我不明白的地方是試圖顯示其中一個視頻縮略圖,作爲我選擇的佈局的一部分。
問題
我想從第一media:thumbnail
元搶URL
屬性的值在img
標籤的使用,但無論我嘗試,我似乎無法呈現它的內容。
例如; <media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/default.jpg'... />
的一段文字,我想從上面的例子中提取&使用是http://i.ytimg.com/vi/CQP_AuT4zXQ/default.jpg
我懷疑,爲什麼我不能解析此元素的值有事情做與元素名稱中包含句點(:)分隔符,和/或我對jQuery的知識懷疑我沒有調用或試圖正確解析此元素或其中的一部分。
我也想呈現media:keywords
的值,這可以通過理解相同的問題來解決。
代碼用來渲染XML
<script type="text/javascript">
/* 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("<h2><a href=\"" + $(this).find("link").text() + "\" target=\"_blank\"> " + $(this).find("title").text() + "</a></h2>\n");
//this is a new paragraph with the publication date
$("#output").append("<p>" + $(this).find("pubDate").text() + "</p>\n");
//this is the description text of the item
$("#output").append("<p>" + $(this).find("description").text() + "</p>\n");
$("#output").append("<p><img src=\"" + $(this).find("media:thumbnail").text() + "\"></p>\n");
});
//end for each
//end function
}
</script>
結果HTML
<h2><a href="http://www.youtube.com/watch?v=CQP_AuT4zXQ&feature=youtube_gdata" target="_blank"> IFA World Forestry Day 2010 forum - Bob Pearce, FIFWA (Part 1)</a></h2>
<p>Thu, 01 Apr 2010 06:44:19 +0000</p>
<p>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.</p>
<p><img src=""></p>
...
XML的樣品從Youtube被解析
文件被抓住並帶到我們的服務器在這裏,以保存文件每次被調用時都會被下載。我們使用VBScript將其轉入網絡,該腳本轉儲從查詢的URL返回的內容。
數據的實施例,從http://gdata.youtube.com/feeds/api/videos?alt=rss&author=fpcwa:
<?xml version='1.0' encoding='UTF-8'?>
<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'>
<channel>
... standard channel description
<item>
<guid isPermaLink='false'>tag:youtube.com,2008:video:CQP_AuT4zXQ</guid>
<pubDate>Thu, 01 Apr 2010 06:44:19 +0000</pubDate>
<atom:updated>2010-04-29T00:03:31.000Z</atom:updated>
<category domain='http://schemas.google.com/g/2005#kind'>http://gdata.youtube.com/schemas/2007#video</category>
<category domain='http://gdata.youtube.com/schemas/2007/categories.cat'>News</category>
<category domain='http://gdata.youtube.com/schemas/2007/keywords.cat'>ifa</category>
... remaining categories
<title>IFA World Forestry Day 2010 forum - Bob Pearce, FIFWA (Part 1)</title>
<link>http://www.youtube.com/watch?v=CQP_AuT4zXQ&feature=youtube_gdata</link>
<author>fpcwa</author>
<yt:accessControl action='comment' permission='allowed'/>
...
<gd:comments>
<gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/CQP_AuT4zXQ/comments?client=ytapi-google-jsdemo' countHint='0'/>
</gd:comments>
<media:group>
<media:category label='News & Politics' scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>News</media:category>
<media:content url='http://www.youtube.com/v/CQP_AuT4zXQ?f=videos&c=ytapi-google-jsdemo&app=youtube_gdata' type='application/x-shockwave-flash' medium='video' isDefault='true' expression='full' duration='587' yt:format='5'/>
<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'/>
<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'/>
<media:credit role='uploader' scheme='urn:youtube'>fpcwa</media:credit>
<media:description type='plain'>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.</media:description>
<media:keywords>ifa, institute of foresters, australia, forestry, world forestry day, 2010, forum, healthy forests, forest, forests, bob pearce, fifwa</media:keywords>
<media:player url='http://www.youtube.com/watch?v=CQP_AuT4zXQ&feature=youtube_gdata'/>
<media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/default.jpg' height='90' width='120' time='00:04:53.500'/>
<media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/2.jpg' height='90' width='120' time='00:04:53.500'/>
<media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/1.jpg' height='90' width='120' time='00:02:26.750'/>
<media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/3.jpg' height='90' width='120' time='00:07:20.250'/>
<media:thumbnail url='http://i.ytimg.com/vi/CQP_AuT4zXQ/hqdefault.jpg' height='360' width='480'/>
<media:title type='plain'>IFA World Forestry Day 2010 forum - Bob Pearce, FIFWA (Part 1)</media:title>
<yt:aspectRatio>widescreen</yt:aspectRatio>
<yt:duration seconds='587'/>
<yt:uploaded>2010-04-01T06:44:19.000Z</yt:uploaded>
<yt:videoid>CQP_AuT4zXQ</yt:videoid>
</media:group>
<yt:statistics favoriteCount='0' viewCount='10'/>
</item>
只是好奇,不會使用[JSON版本的YouTube API](http://code.google.com/apis/youtube/2.0/developers_guide_json.html)簡單得多嗎? :) – 2010-07-02 03:15:11
這個想法已經超越了我的想法,我正在嘗試這兩項任務。對於我來說JSON最大的缺陷之一就是無法讀取JSON數據集,因爲所有的數據都在一行上。 – thewinchester 2010-07-02 03:55:30
只需更改dataType的「xml」:「json」,並且您有一個漂亮的對象。 – levhita 2012-02-28 23:02:46