【问题标题】:Parse string from XML (RSS) <title> only if it contains that string仅当 XML (RSS) <title> 包含该字符串时才解析字符串
【发布时间】:2014-06-22 09:10:38
【问题描述】:

我正在尝试从 RSS 解析一个项目,但前提是它的标题中包含特定字符串。

我有一个 Ember RSS 解析器应用程序,可用作一个游戏的日志检查器。例如,这里显示了一个名为“Versusik”的玩家的“冒险日志”:

http://alog.questscape.eu/#/logs/1/index

这是从包含我正在使用的 RSS 提要的 URL 中抓取信息的代码。

“feed.js”:

App.Feed = DS.Model.extend({
  name: DS.attr('string'),
  url: DS.attr('string'),

  feedItems: DS.hasMany('App.FeedItem'),

  refresh: function() {
    var url = this.get('url');
    var googleUrl = document.location.protocol + '//ajax.googleapis.com/ajax/services/feed/load?v=1.0&num=10&callback=?&q=http://services.runescape.com/m=adventurers-log/k=4/rssfeed?searchName=' + encodeURIComponent(url);
    $.ajax({
      url: googleUrl,
      dataType: 'json',
      context: this,
      success: function(data) {
        var feed = data.responseData.feed;
        var items = feed.entries.forEach(function(entry) {
          if(this.get('feedItems').findProperty('link', entry.link)) {
            return;
          }
          var feedItem = this.get('feedItems').createRecord({
            title: entry.title,
            author: entry.author,
            body: entry.content,
            bodySnippet: entry.contentSnippet,
            link: entry.link,
            publishedDate: entry.publishedDate
          });
        }, this);
        this.get('store').commit();
      }
    });
  }
});

App.Feed.FIXTURES = [
  {id: 1, name: 'Versusik', url: 'Versusik'},
  {id: 2, name: 'Questalion', url: 'Questalion'},
  {id: 3, name: 'Mokuin', url: 'Mokuin'},
  {id: 4, name: 'Aiimer', url: 'Aiimer'},
  {id: 5, name: 'Jakube33', url: 'Jakube33'},
  {id: 6, name: 'Mordor Prime', url: 'Mordor_Prime'},
  //...
  //...it goes on for every Player I've specified...
  //...
  {id: 53, name: 'TooShortName', url: 'TooShortName'},

];

这里是单个项目的代码。

“feed_item.js”:

App.FeedItem = DS.Model.extend({
  title: DS.attr('string'),
  author: DS.attr('string'),
  content: DS.attr('string'),
  contentSnippet: DS.attr('string'),
  link: DS.attr('string'),
  publishedDate: DS.attr('date'),

  feed: DS.belongsTo('App.Feed')
});

App.FeedItem.FIXTURES = []; 

它按预期工作,从 http://services.runescape.com/m=adventurers-log/rssfeed?searchName=("Name of player", you can try Versusik) 解析数据并将其显示在我的 ember 应用程序中。

但是如果我只想解析标题包含字符串“dragon helm”的项目(而完整的项目标题显示“I found a dragon helm”)怎么办?

基本上我想制作类似的应用程序,但它只会解析这些字符串并将其放在像this 这样的表中。

提前感谢您提供的任何帮助。

【问题讨论】:

  • 您可能需要使用 filterBy 或类似方法过滤结果。

标签: javascript xml parsing ember.js rss


【解决方案1】:

但是如果我只想解析标题包含字符串“dragon helm”的项目(而完整的项目标题显示“I found a dragon helm”)怎么办?

使用命名函数来分离关注点:

function foo(data)
  {
  /* feed data */
  var feed = data.responseData.feed;
  /* feed entry transformation */
  var items = feed.entries.forEach(function(entry) {bar(entry, this)}); 

  this.get('store').commit();
  }

function bar(entry, that)
  {
  if(that.get('feedItems').findProperty('link', entry.link) == false) 
    {
    return;
    }
  else
    {
    that.get('feedItems').createRecord({
        title: entry.title,
        author: entry.author,
        body: entry.content,
        bodySnippet: entry.contentSnippet,
        link: entry.link,
        publishedDate: entry.publishedDate
      });
    }
  }

然后将条件检查添加到成功回调中:

success: function(data) {
  /* Test for "dragon helm" in stringified JSON */
  if /dragon\shelm/.test(JSON.stringify(data))
    {
    foo(data);
    }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-29
    • 1970-01-01
    • 1970-01-01
    • 2013-05-31
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    相关资源
    最近更新 更多