3

我正在尝试使用 Google 的 Feed API 从 DeviantArt 提供的 RSS 提要中获取媒体,以 JSON 格式返回。饲料如下:

http://backend.deviantart.com/rss.xml?type=deviation&q=boost%3Apopular%20tarutaru

谷歌提供了很多例子,但很少涉及媒体提要,而那些这样做的,都使用了媒体组。但是看着我试图阅读的这个特定的提要,我没有看到任何媒体组。每个提要条目只有一个与之关联的“媒体”项目。

<item>
<title>Expectant Tarutaru</title>
<link>http://requiem-shade.deviantart.com/art/Expectant-Tarutaru-38312114</link>
<guid isPermaLink="true">http://requiem-shade.deviantart.com/art/Expectant-Tarutaru-38312114</guid>
<pubDate>Sat, 19 Aug 2006 17:20:08 PDT</pubDate>
<media:title type="plain">Expectant Tarutaru</media:title>
<media:keywords/>
<media:rating>nonadult</media:rating>
<media:category label="Games">fanart/traditional/drawings/games</media:category>
<media:credit role="author" scheme="urn:ebu">Requiem-Shade</media:credit>
<media:credit role="author" scheme="urn:ebu">http://a.deviantart.net/avatars/r/e/requiem-shade.jpg</media:credit>
<media:copyright url="http://requiem-shade.deviantart.com">Copyright 2006-2012 !Requiem-Shade</media:copyright>        
<media:description type="html"><![CDATA[ A pregnant Tarutaru White Mage.  Was a request from another board.  Yes, I took liberties with Square-Enix's original design.  Yes, Tarutaru look like that as adults, except the females are flat-chested.  Had to augment her form to reflect the development of her body as her children grow inside her. ]]></media:description>
<media:thumbnail url="http://th06.deviantart.net/fs11/150/i/2006/231/c/f/Expectant_Tarutaru_by_Requiem_Shade.jpg" height="150" width="140"/>            
<media:thumbnail url="http://th03.deviantart.net/fs11/300W/i/2006/231/c/f/Expectant_Tarutaru_by_Requiem_Shade.jpg" height="322" width="300"/>
<media:content url="http://fc06.deviantart.net/fs11/i/2006/231/c/f/Expectant_Tarutaru_by_Requiem_Shade.jpg" height="463" width="431" medium="image"/>        
<media:content url="http://www.deviantart.com/download/38312114/" medium="document"/>
<description><![CDATA[ A pregnant Tarutaru White Mage.  Was a request from another board.  Yes, I took liberties with Square-Enix's original design.  Yes, Tarutaru look like that as adults, except the females are flat-chested.  Had to augment her form to reflect the development of her body as her children grow inside her.<br /><div><img src="http://th03.deviantart.net/fs11/300W/i/2006/231/c/f/Expectant_Tarutaru_by_Requiem_Shade.jpg" alt="thumbnail" /></div> ]]></description>
</item>

我现在用来读取提要的代码是这样的,现在只是尝试读取media:title值,但我想读取所有media:数据。

/*
*  How to load a feed via the Feeds API.
*/

google.load("feeds", "1");

// Our callback function, for when a feed is loaded.
function feedLoaded(result) {
    if (!result.error) {
        // Grab the container we will put the results into
        var container = document.getElementById("content");
        container.innerHTML = '';

        // Loop through the feeds, putting the titles onto the page.
        // Check out the result object for a list of properties returned in each entry.
        // http://code.google.com/apis/ajaxfeeds/documentation/reference.html#JSON
        for (var i = 0; i < result.feed.entries.length; i++) {
            var entry = result.feed.entries[i];
            var div = document.createElement("div");
            div.appendChild(document.createTextNode(entry.mediaTitle));
            container.appendChild(div);
        }
    }
}

function OnLoad() {
    // Create a feed instance that will grab Digg's feed.
    var feed = new google.feeds.Feed("http://backend.deviantart.com/rss.xml?type=deviation&q=boost%3Apopular%20tarutaru");

    // Calling load sends the request off.  It requires a callback function.
    feed.load(feedLoaded);
}

google.setOnLoadCallback(OnLoad);​

取自此处提供的示例:http ://code.google.com/apis/ajax/playground/#load_feed

问题是,我不知道达到这些值的正确语法是什么,而且我已经尝试了几个小时,所以你现在是我唯一的希望。

任何帮助将不胜感激。这是我为我兄弟做的生日礼物,我想尽快摆脱这种麻烦,以便送出礼物。:)

先感谢您。

4

1 回答 1

0

寻找阅读 XML 提要的示例(我手边没有 URL)。这就是我为解决这个问题所做的:我在调用 feed.load() 之前将格式设置为 XML:feed.setResultFormat(google.feeds.Feed.XML_FORMAT); 然后,在回调中,我遍历提要中的项目(看起来你可以对你的项目做同样的事情,因为它作为项目返回)

function feedLoaded(result) {
  if (!result.error) {
  // Get all items returned.
  var items = result.xmlDocument.getElementsByTagName('item');

  // Loop through our items
  for (var i = 0; i < items.length; i++) {
    var item = items[i];

    // Get the data from the element.  firstChild is the text node containing
    // the title, and nodeValue returns the value of it.
    var title = item.getElementsByTagName('title')[0].firstChild.nodeValue;
    var link = item.getElementsByTagName('link')[0].firstChild.nodeValue;
}

这些应该对您有效,因为您的提要也有标题和链接。遵循相同的基本方案来获取其余数据。

于 2012-11-15T16:23:32.157 回答