Hello there,
I am trying to use XML API for making RSS, I need to convert XML to JSON, but I have some problems, could you help me to solve them.
home.ts
import { NavController } from 'ionic-angular';
import { Component } from '@angular/core';
import { RssService } from '../../providers/rss-servise';
import { DetailPage } from '../detail-page/detail-page';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [RssService]
})
export class HomePage {
public entries: any = []; /* this needs to be class level variable */
constructor(public rssService:RssService, public nav:NavController) {
}
ionViewDidLoad(){
this.rssService.load().subscribe(
data => {
this.entries.push(data); /* push retrieved data into the array */
}
);
}
openPage(entry) {
console.log('open page called with '+ entry.title);
this.nav.push(DetailPage, {selectedEntry:entry});
}
}
rss-servise.ts
import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';
import {Observable} from 'rxjs/Observable';
import * as xml2js from "xml2js"
@Injectable()
export class RssService {
constructor(public http: Http) {
console.log('Hello RssService Provider');
}
load() {
return Observable.create(s => {
this.http.get('https://lenta.ru/rss/top7').map(res => {
let data = res.json();
xml2js.parseString(data, ((result) => {
console.log(result);
return result;
}));
})
.subscribe(result => {
console.log(result);
var items = data.query.results.item;
items.forEach(i=>s.next(i));
s.complete();
});
});
}
}
detail-page.ts
import {NavController, NavParams} from 'ionic-angular';
import { Component } from '@angular/core';
@Component({
selector: 'detail-page',
templateUrl: 'detail-page.html',
})
export class DetailPage {
entry:any = {};
constructor(public nav: NavController, navParams:NavParams) {
console.log('run');
this.nav = nav;
this.entry = navParams.get('selectedEntry');
console.log('my entry is '+ this.entry.title);
}
}
Thank you in advance.