Leveraging RSS XML data in Angular by parsing it with x2js and displaying it using ng-repeat

Hey there, I could really use some assistance. I've been trying to solve this puzzle for the past few days without much luck.

My goal is to extract only the item objects from an XML RSS file using Angular and x2js. The structure of the XML file looks like this:

<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 have successfully retrieved the results, but when attempting to use ng-repeat in my markup, it's iterating over everything in the channel section, resulting in empty list objects being displayed. Here's a snippet of my HTML:

<html ng-app="myApp">

  <head>
   <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="17767970627b7665397d64572339273927">[email protected]</a>" 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>

Below are my script files:

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);
  });

});

You can access the Plunkr demo here: Plunker Demo

Answer №1

Apologies for the delay in closing this issue. I am providing an update just in case it may benefit others facing a similar problem. To resolve the issue, I modified my html code by eliminating the "ng-repeat" attribute from the ul tag and inserting the li ng-repeat instead. Furthermore, I made adjustments to the script by changing $scope.list = response; to $scope.list = response.data;. This adjustment allowed me to target only the items within the channel tag in the rss, avoiding iterating over unnecessary elements. Refer to the updated code below and explore the changes on this refreshed plunkr.

    <html ng-app="myApp">
         <head>
          <script data-require="<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="d8b9b6bfadb4b9aaf6b2ab98ecf6e8f6e8">[email protected]</a>" 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>

Below is the revised 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);
      });

    });

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I override the default success_url and redirect to a custom page in Stripe Checkout?

QUESTION: I have been experiencing an issue with a webhook in my Node.js application. When the event 'checkout.session.completed' is triggered, the user is automatically redirected to the success_url, regardless of my attempts to redirect them t ...

What advantages can be found in using various CRUD techniques?

When working with CRUD operations, the process can be done using a form like this: <form action="/todo/<%= todos[i]._id %>?_method=DELETE" method="POST> <button>x</button> </form> The corresponding con ...

The button in the fixed bottom navbar on Android is not responsive to the ng-click or onclick events

I am developing a mobile application using mobile angular ui. I am facing an issue where I am unable to click on a button at the bottom of the screen when testing on an android device, even though it works fine in a browser. It appears that the clickable a ...

Unable to establish socket.io connection on the client side

I'm struggling to connect to socket.io on the client side. While investigating the issue, I came across various script notations: <script src="socket.io.js"></script> <script src="/socket-lib/socket.io.js"></script> <script ...

Instructions for sending a grouping of items to a WebApi utilizing Restangular.Js

When the departments are null, this data is sent to the server: Here is the JavaScript code snippet: var departments = [{ DepartmentName : "Name", DepartmentId : 1}]; Restangular.all('records/StartNewDate').post(departments); This is how it is ...

What is the best way to incorporate three.js with Cesium, or is it better the other

I recently completed rendering 3D objects (JSON and OBJ models) using the Three.js framework. I am now looking to render the scene in the Cesium framework based on Geo Coordinates. Has anyone attempted this integration before? Any insights, articles, or s ...

Is it possible to pass a class method to an onClick event from within the render method in ReactJS?

Excuse my lack of experience, but I haven't been able to find a solution to this yet. I am attempting to utilize a class method as a callback for the onClick event in my JSX. Below is the code for my App component: import React from 'react&apo ...

Increasing space at the top with heading

As I scroll down, the header on my website remains in a static position and disappears. However, when I scroll back up, the header reappears wherever the user is on the page. While this functionality works well, I have noticed that as I scroll all the way ...

Executing numerous HTTP requests within a single Angular JS controller

After creating multiple Drupal views to generate various JSON endpoints for different HTTP requests using Angular, I find myself in a situation where I have separate controllers for each request in my Angular script (as shown below). However, I am now lo ...

The MTM test runner is failing to identify AJAX callbacks

During my manual testing, I encountered an issue where the recorded test would get stuck after a button click and not proceed to the next page, causing the test to fail. This problem also occurred in CUIT, but I was able to resolve it using Ross McNab&apos ...

click on a link to submit a form that is not part of

I've been struggling to understand why my form is not submitting properly. Any assistance would be greatly appreciated. What I'm attempting to do is submit my form using an external link. Here is the HTML code snippet: <li><a href="#" ...

Expanding the scroll view by incorporating buttons in a relative layout (or linear layout) from top to bottom

Seeking assistance with a programming challenge. I'm currently working on a feature where a single button is displayed at the bottom of the screen. Each time the counter++ button is clicked, a new button should be dynamically created above the existi ...

Is there a way to verify and send notifications when duplicate entries are added to my table?

Whenever a make and model are added using the "add" button, I need to ensure that there are no duplicates. If a duplicate is found, an alert should be displayed, and the entry should not be added to the table. Unfortunately, I have been unable to find a so ...

Validation error detected in the textbox fields

I have incorporated Bootstrap into my application. For a specific functionality, I am dynamically creating dropdown and textbox controls based on a count and implementing validation. However, the validation seems to be malfunctioning only for the textbox ...

Modify the color of the text input by the user in an AJAX-enhanced text box

After successfully implementing an autocomplete textbox using AJAX Autocomplete, I decided to enhance the feature with some Fuzzy logic. Now, as the user enters 3 characters, my database returns a list of records that match those characters. The search re ...

Increase the count for each category with the help of JavaScript

Currently, I am working on an application using PHP and have 180 vendors registered in the database. Each vendor has a unique ID stored in the database. I need to create a system where every time a user clicks on the "view details" button for a specific ...

Encountering an Unspecified Error in JavaScript while rapid-clicking on a RadGrid with a DetailTable

When testing, I am using Internet Explorer 11. Within a RadGrid control, I have a list of Groups (groupId, groupName) displayed. Each Group includes a DetailTable with the GroupMembers (memberId, groupId, memberName). The RadGrid is located in a Content ...

Attempting to output properties from an Express/Mongo API by utilizing a React.js frontend

I am currently in the process of developing a simplistic fictional sneaker application with the MERN stack. While I wouldn't classify myself as a beginner, I'm also not an expert. I successfully created the backend and generated a json rest-api. ...

Transferring information to the specified link using AJAX and PHP

I'm having trouble sending data to a link that I click on. Whenever I try, I just end up with an undefined index error in my PHP file. <a href="output.php"> Click me </a> ('a').click(function(){ href = "output.php"; ...

The outcome of the mongoose .exists function in JavaScript is a boolean value

Currently, I am delving into the world of node.js and mongoDB. For a project I'm working on, I've set up a demonstration login system to practice my skills. In my main API, I have a function that checks whether a refreshToken exists in the mongo ...