获取前缀:来自 XML RSS 提要的项目值

Posted

技术标签:

【中文标题】获取前缀:来自 XML RSS 提要的项目值【英文标题】:Getting a prefix: item value from an XML RSS feed 【发布时间】:2020-11-26 21:50:14 【问题描述】:

我正在尝试在客户端使用 JQuery 解析 XML。这是a Youtube channel RSS feed 的 sn-p / 示例:

  <published>2020-08-05T19:00:01+00:00</published>
  <updated>2020-08-05T22:20:16+00:00</updated>
  <media:group>
   <media:title>Ethan Gruska &quot;On The Outside&quot; - Late Show #PlayAtHome</media:title>
   <media:content url="https://www.youtube.com/v/8YPCM511lWQ?version=3" type="application/x-shockwave-flash"  />
   <media:thumbnail url="https://i1.ytimg.com/vi/8YPCM511lWQ/hqdefault.jpg"  />
   <media:description>Singer, songwriter and producer Ethan Gruska treats us to this #PlayAtHome performance of &quot;On The Outside,&quot; from his new album &quot;EN GARDE&quot; which is available now on Warner Records. #EthanGruska #OnTheOutside #PlayAtHome

我正在遍历整个文件,将所有 &lt;item&gt; 放入 item 变量中。

$.each(data.feedItems, function(index, item) 

    console.log('(%s) %s', feedUrl, JSON.stringify(item) + '\n');

(...)

这是上面的输出,您可以在其中看到我所追求的“描述”值:

07:49:58.627 (https://www.youtube.com/feeds/videos.xml?channel_id=UC3I2GFN_F8WudD_2jUZbojA) "title":"Chicano Batman (Live on KEXP at Home)","description":null,"summary":null,"date":"2020-08-07T01:25:49.000Z","pubdate":"2020-08-06T23:10:23.000Z","pubDate":"2020-08-06T23:10:23.000Z","link":"https://www.youtube.com/watch?v=jZhylCfqc9E","guid":"yt:video:jZhylCfqc9E","author":"KEXP","comments":null,"origlink":null,"image":"url":"https://i3.ytimg.com/vi/jZhylCfqc9E/hqdefault.jpg","source":,"categories":[],"enclosures":[],"atom:@":,"atom:id":"@":,"#":"yt:video:jZhylCfqc9E","yt:videoid":"@":,"#":"jZhylCfqc9E","yt:channelid":"@":,"#":"UC3I2GFN_F8WudD_2jUZbojA","atom:title":"@":,"#":"Chicano Batman (Live on KEXP at Home)","atom:link":"@":"rel":"alternate","href":"https://www.youtube.com/watch?v=jZhylCfqc9E","atom:author":"@":,"name":"@":,"#":"KEXP","uri":"@":,"#":"https://www.youtube.com/channel/UC3I2GFN_F8WudD_2jUZbojA","atom:published":"@":,"#":"2020-08-06T23:10:23+00:00","atom:updated":"@":,"#":"2020-08-07T01:25:49+00:00","media:group":"@":,"media:title":"@":,"#":"Chicano Batman (Live on KEXP at Home)","media:content":"@":"url":"https://www.youtube.com/v/jZhylCfqc9E?version=3","type":"application/x-shockwave-flash","width":"640","height":"390","media:thumbnail":"@":"url":"https://i3.ytimg.com/vi/jZhylCfqc9E/hqdefault.jpg","width":"480","height":"360","media:description":"@":,"#":"LA's Chicano Batman share a set of songs recorded exclusively for KEXP and join Morgan to talk live on Thursday, August 6, at 3pm PT.","media:community":"@":,"media:starrating":"@":"count":"418","average":"4.82","min":"1","max":"5","media:statistics":"@":"views":"7909","meta":"#ns":["xmlns:yt":"http://www.youtube.com/xml/schemas/2015","xmlns:media":"http://search.yahoo.com/mrss/","xmlns":"http://www.w3.org/2005/Atom"],"@":["xmlns:yt":"http://www.youtube.com/xml/schemas/2015","xmlns:media":"http://search.yahoo.com/mrss/","xmlns":"http://www.w3.org/2005/Atom"],"#xml":"version":"1.0","encoding":"UTF-8","#type":"atom","#version":"1.0","title":"KEXP","description":null,"date":"2006-07-07T16:39:26.000Z","pubdate":"2006-07-07T16:39:26.000Z","pubDate":"2006-07-07T16:39:26.000Z","link":"https://www.youtube.com/channel/UC3I2GFN_F8WudD_2jUZbojA","xmlurl":"http://www.youtube.com/feeds/videos.xml?channel_id=UC3I2GFN_F8WudD_2jUZbojA","xmlUrl":"http://www.youtube.com/feeds/videos.xml?channel_id=UC3I2GFN_F8WudD_2jUZbojA","author":"KEXP","language":null,"favicon":null,"copyright":null,"generator":null,"cloud":,"image":,"categories":[],"atom:@":"xmlns:yt":"http://www.youtube.com/xml/schemas/2015","xmlns:media":"http://search.yahoo.com/mrss/","xmlns":"http://www.w3.org/2005/Atom","atom:link":["@":"rel":"self","href":"http://www.youtube.com/feeds/videos.xml?channel_id=UC3I2GFN_F8WudD_2jUZbojA","@":"rel":"alternate","href":"https://www.youtube.com/channel/UC3I2GFN_F8WudD_2jUZbojA"],"atom:id":"@":,"#":"yt:channel:UC3I2GFN_F8WudD_2jUZbojA","yt:channelid":"@":,"#":"UC3I2GFN_F8WudD_2jUZbojA","atom:title":"@":,"#":"KEXP","atom:author":"@":,"name":"@":,"#":"KEXP","uri":"@":,"#":"https://www.youtube.com/channel/UC3I2GFN_F8WudD_2jUZbojA","atom:published":"@":,"#":"2006-07-07T16:39:26+00:00"
PTL.feed.js:269:17

我正在尝试访问 media:group 元素;我可以得到***的,比如item.published,但不是item['media:group']['media:description'],因为它总是返回[object Object]:|

我可以用什么方法来读取这个值?

【问题讨论】:

阅读How to Ask。邮政编码作为minimal reproducible example 的一部分。 【参考方案1】:

在您的示例中,媒体:描述是这样的:

"media:description": 
            "@": ,
            "#": "LA's Chicano Batman share a set of songs recorded exclusively for KEXP and join Morgan to talk live on Thursday, August 6, at 3pm PT."
        

这是一个对象。 (因此你的结果) 如果您想要文字,请尝试

item['media:group']['media:description']['#']

【讨论】:

是的,我终于想通了,谢谢;不知道这个“#”是从哪里来的,因为它不在原始 RSS 文件中……很奇怪。 #是从xml到json的映射。 # 保存元素的文本值。

以上是关于获取前缀:来自 XML RSS 提要的项目值的主要内容,如果未能解决你的问题,请参考以下文章

如何获取 RSS 提要上的所有旧项目?

C# - 如何从 xml 中获取特定值

Google 阅读器如何获取 RSS 提要中的每个项目?

sh 监视RSS提要以获取来自bash的更改

在 PHP 中从 Google RSS 提要 xml 中获取前 9 个元素

获取 RSS 提要时未定义