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

Getting a prefix: item value from an XML RSS feed

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

  <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" width="640" height="390"/>
   <media:thumbnail url="https://i1.ytimg.com/vi/8YPCM511lWQ/hqdefault.jpg" width="480" height="360"/>
   <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

我正在遍历整个文件,将所有 <item> 放入 item var.

$.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'],因为它总是 returns [object Object] :|

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

在您的示例中,media:description 是这样的:

"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']['#']