解析AngularJS提要中的xml标记

Parse xml tag in a AngularJS feed

本文关键字:xml 标记 AngularJS 解析      更新时间:2023-09-26

我需要解析enclosure标记才能获得url图像。假设我应该使用json+xml代码获得MIXED OUTPUT,但当我尝试解析它时,我从enclosure标记中获得了一个未定义的值。我这样做就像我在这篇文章中看到的那样>Google Feed Loader API忽略xml属性<此外,我试图让MIXED格式手动编写url,但它不起作用。这是我的全部密码。我怎么知道我得到的是混合json输出呢?

    var feeds = [];
var entryImageUrl = [];
angular.module('starter.controllers', ['ngResource','ngLocale'])
.factory('FeedLoader', function ($resource) {
        return $resource('http://ajax.googleapis.com/ajax/services/feed/load', {}, {
            fetch: { method: 'JSONP', params: {v: '1.0', callback: 'JSON_CALLBACK', output: 'json_xml'} }
        });
})
.service('FeedList', function ($rootScope, FeedLoader) {
    this.get = function() {
        var feedSources = [
            {title: 'Heraldo De Barbate', url: 'http://www.heraldodebarbate.es/rss/last'},
        ];
        if (feeds.length === 0) {
            for (var i=0; i<feedSources.length; i++) {
                FeedLoader.fetch({q: feedSources[i].url, num: 10}, {}, function (data) {
                    var feed = data.responseData.feed;
                    **var entryImageUrl = feed.xmlNode.getElementsByTagName("enclosure")[i].getAttribute("url");**
                    feeds.push(feed);
                });
            }
        }
        return feeds;
    };
})
.controller('FeedCtrl', function ($scope, FeedList,$timeout) {
  $scope.update = function(){
    $scope.feeds = FeedList.get();
    $scope.$on('FeedList', function (event, data) {
        $scope.feeds = data;
        // $scope.entryImageUrl 
        console.log(feeds);
    });
    $timeout(function() {
      $scope.$broadcast('scroll.refreshComplete');
    }, 500);    
  }
})

我怎么知道我得到的是混合json输出呢?

对JSON:中的标签使用测试

function testMe(node)
  {
  return /</.test(JSON.stringify(node) )
  }

然后在提要上运行:

var mixed_format = testMe(feed);

并调用另一个解析数据的函数:

if (mixed_format)
  {
  mixed_parser(feed)
  }
else
  {
  json_parser(feed)
  }