2010-07-22 91 views
0

我正在解析AIR應用中的YouTube視頻源。 使用jquery解析它。Jquery無法解析XML

$(feed).find("entry").each(function() 
    { 
     var tit = $(this).find('title'); 
     alert(tit.text()); 
    } 

好像解析category(類別標籤被關閉以不同的方式)後,它無法解析上面的代碼doc.The休息成功警報文本,直到updated tag.What可以解。

<entry gd:etag='W/&quot;C0AMRn47eCp7ImA9WxRQGUw.&quot;'> 
     <id>tag:youtube,2008:video:ZTUVgYoeN_b</id> 
     <published>2008-07-05T19:56:35.000-07:00</published> 
     <updated>2008-07-18T07:21:59.000-07:00</updated> 
     <category scheme='http://schemas.google.com/g/2005#kind' 
      term='http://gdata.youtube.com/schemas/2007#video'/> 
     <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' 
      term='Shopping'/> 
     <category scheme='http://gdata.youtube.com/schemas/2007/keywords.cat' 
      term='parkas'/> 
     <category scheme='http://gdata.youtube.com/schemas/2007/categories.cat' 
      term='People' label='People'/> 
     <title>Shopping for Coats</title> 
     <content type='application/x-shockwave-flash' 
      src='http://www.youtube.com/v/ZTUVgYoeN_b?f=gdata_standard...'/> 
     <link rel='alternate' type='text/html' 
      href='http://www.youtube.com/watch?v=ZTUVgYoeN_b'/> 
     <link rel='http://gdata.youtube.com/schemas/2007#video.responses' 
      type='application/atom+xml' 
      href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/responses?v=2'/> 
     <link rel='http://gdata.youtube.com/schemas/2007#video.ratings' 
      type='application/atom+xml' 
      href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/ratings?v=2'/> 
     <link rel='http://gdata.youtube.com/schemas/2007#video.complaints' 
      type='application/atom+xml' 
      href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/complaints?v=2'/> 
     <link rel='http://gdata.youtube.com/schemas/2007#video.related' 
      type='application/atom+xml' 
      href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/related?v=2'/> 
     <link rel='http://gdata.youtube.com/schemas/2007#mobile' 
      type='text/html' href='http://m.youtube.com/details?v=ZTUVgYoeN_b'/> 
     <link rel='self' type='application/atom+xml' 
      href='http://gdata.youtube.com/feeds/api/standardfeeds/top_rated/v/ZTUVgYoeN_b?v=2'/> 
     <author> 
      <name>andyland74</name> 
      <uri>http://gdata.youtube.com/feeds/api/users/andyland74</uri> 
     </author> 
     <media:group> 
      <media:title type='plain'>Shopping for Coats</media:title> 
      <media:description type='plain'> 
      What could make for more exciting video? 
      </media:description> 
      <media:keywords>Shopping, parkas</media:keywords> 
      <yt:duration seconds='79'/> 
      <yt:videoid>ZTUVgYoeN_b</yt:videoid> 
      <media:player url='http://www.youtube.com/watch?v=ZTUVgYoeN_b'/> 
      <media:credit role='uploader' scheme='urn:youtube'>andyland74</media:credit> 
      <media:category label='People' 
      scheme='http://gdata.youtube.com/schemas/2007/categories.cat'>People 
      </media:category> 
      <media:content 
      url='http://www.youtube.com/v/ZTUVgYoeN_b?f=gdata_standard...' 
      type='application/x-shockwave-flash' medium='video' 
      isDefault='true' expression='full' duration='215' yt:format='5'/> 
      <media:content 
      url='rtsp://rtsp2.youtube.com/ChoLENy73bIAEQ1kgGDA==/0/0/0/video.3gp' 
      type='video/3gpp' medium='video' 
      expression='full' duration='215' yt:format='1'/> 
      <media:content 
      url='rtsp://rtsp2.youtube.com/ChoLENy73bIDRQ1kgGDA==/0/0/0/video.3gp' 
      type='video/3gpp' medium='video' 
      expression='full' duration='215' yt:format='6'/> 
      <media:thumbnail url='http://img.youtube.com/vi/ZTUVgYoeN_b/2.jpg' 
      height='97' width='130' time='00:00:03.500'/> 
      <media:thumbnail url='http://img.youtube.com/vi/ZTUVgYoeN_b/1.jpg' 
      height='97' width='130' time='00:00:01.750'/> 
      <media:thumbnail url='http://img.youtube.com/vi/ZTUVgYoeN_b/3.jpg' 
      height='97' width='130' time='00:00:05.250'/> 
      <media:thumbnail url='http://img.youtube.com/vi/ZTUVgYoeN_b/0.jpg' 
      height='240' width='320' time='00:00:03.500'/> 
     </media:group> 
     <yt:statistics viewCount='383290' favoriteCount='7022'/> 
     <gd:rating min='1' max='5' numRaters='14763' average='4.93'/> 
     <georss:where> 
      <gml:Point> 
      <gml:pos>21.37124437061831 -157.87353515625</gml:pos> 
      </gml:Point> 
     </georss:where> 
     <gd:comments> 
      <gd:feedLink href='http://gdata.youtube.com/feeds/api/videos/ZTUVgYoeN_b/comments' 
      countHint='9416'/> 
     </gd:comments> 
     </entry> 
+0

什麼瀏覽器和什麼版本不工作?另外,這不是你解析XML的方式。 – Anurag 2010-07-23 01:17:30

+0

不是瀏覽器,它是Adobe Air App.Well讓我知道如何解析XML,那就是我在這裏問的! – Arshdeep 2010-07-23 06:39:35

回答

1

你說得對類別標籤...奇怪的事情做,我注意到這一點,當我複製並粘貼註冊爲文字谷歌旗下YouTube的XML代碼,因爲它是<pre style="word-wrap: break-word; white-space: pre-wrap;">[XML HERE]</pre>內部分成XML解析器;它也無法閱讀類別標籤,這似乎是不正確的關閉(即使它們是)。我有一種感覺,以修正XML,斜槓和大於號是必要之間的空間,即

<category scheme='[scheme_url]' term='[term_name]' />

,而不是他們有什麼:

<category scheme='[scheme_url]' term='[term_name]'/>

但我想這並不能幫助你解決你的問題,除非你願意先修復XML。不過,我已經使用jQuery成功解析了相同的提要。這可能是您的初始$.get代碼,如果您尚未使用$.getJSON,請嘗試使用...如果您仍需要幫助,請讓我看看代碼的其餘部分。

否則,這是我一直在使用的,它確實工作(據我所知)。我在網站中使用它,而不是Adobe AIR,但如果您在AIR中使用jQuery,那麼jQuery就是jQuery,它應該沒關係(這是關於jQuery,imho最好的部分)。這裏是我的代碼:

$.getJSON("http://gdata.youtube.com/feeds/api/users/[insert_userName_here]/uploads?orderby=published&start-index=1&max-results=50&v=2&alt=json-in-script&callback=?", 
function(json){ 
    $.each(json.feed.entry,function(j,entry) { 
     //Note: I wouldn't use an alert if you're reading from a big feed, as max-results is set to 50... 
     alert(entry.title.$t); 
    }); 
}); 

上面的代碼讀取一個包含所有上傳視頻的任何一個用戶的飼料。希望這可以幫助!