Hi hoping someone can help. I've been racking my brain for a few days trying to figure this out.
I'm trying to parse an xml rss file with angular and x2js to return only the item objects. It's formatted like:
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
<channel>
<title>Test Title</title>
<link>
https:somelocation.com
</link>
<description>List Title 1</description>
<pubDate>Wed, 26 Apr 2017 13:04:51 GMT</pubDate>
<dc:date>2017-04-26T13:04:51Z</dc:date>
<item>
<title>Test Title 1</title>
<link>
https://plnkr.co
</link>
<description>
<DIV> <P>Some description text</P> </DIV>
</description>
<pubDate>Wed, 26 Apr 2017 13:04:51 GMT</pubDate>
<guid>
https://plnkr.co
</guid>
<dc:date>2017-04-26T13:04:51Z</dc:date>
</item>
</channel>
</rss>
I can return results but when I try to ng-repeat in my markup it's iterating over everything in the channel causing empty list objects to appear in the results. Here is my html:
<html ng-app="myApp">
<head>
<script data-require="angular.js@4.0.0" data-semver="4.0.0"
src="https://code.angularjs.org/latest/angular.min.js"></script>
<script src="xml2Json.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller="listController">
<ul ng-repeat="item in list">
<li ng-repeat="i in item">
<h2><a href="{{i.link}}">{{i.title}}</a></h2>
</li>
</ul>
</body>
</html>
And here are my scripts:
var myApp = angular.module('myApp', []);
myApp.controller('listController', function($scope,$http){
$http.get('rssFeed.xml', {
transformResponse: function (data) {
var x2js = new X2JS();
var listData = x2js.xml_str2json(data);
return listData.rss.channel.item;
}
}).then(function successCallback(response, status){
$scope.list = response;
}, function errorCallback(response, status){
console.log('Request failed' + status);
});
});
The plunkr is located here: plunker
I forgot to return and close this. Answering in case it's helpful to anyone else running into this issue. I updated my html by removing "ng-repeat" attribute from the ul tag and replaced the li ng-repeat with it. I also updated the the script by changing $scope.list = response;
to $scope.list = response.data;
. By doing this I was able to avoid iterating over everything under the channel tag in the rss and only target the items themselves. See code below and here's an updated plunkr .
<html ng-app="myApp">
<head>
<script data-require="angular.js@4.0.0" data-semver="4.0.0"
src="https://code.angularjs.org/latest/angular.min.js"></script>
<script src="xml2Json.js"></script>
<link rel="stylesheet" href="style.css" />
<script src="script.js"></script>
</head>
<body ng-controller="listController">
<ul>
<li ng-repeat="item in list">
<h2><a href="{{i.link}}">{{i.title}}</a></h2>
</li>
</ul>
</body>
</html>
Here is my updated script
var myApp = angular.module('myApp', []);
myApp.controller('listController', function($scope,$http){
$http.get('rssFeed.xml', {
transformResponse: function (data) {
var x2js = new X2JS();
var listData = x2js.xml_str2json(data);
return listData.rss.channel.item;
}
}).then(function successCallback(response, status){
$scope.list = response.data;
}, function errorCallback(response, status){
console.log('Request failed' + status);
});
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.