-1

My code is always printing error log, how can i find the exception please tell me the right way to this Here is my code

this.http.get('http://www.gazetaexpress.com/rss/sport/?xml=1').subscribe(data => {
    this.xmlItems = data;
    console.log("data:"+data);
    xml2js.parseString(this.xmlItems, function (err, result) {
        console.log(result);
    });
}, error => {
    console.log(error);
});
3
  • did you access this URL in browser. its returning error? Commented Oct 12, 2018 at 12:48
  • 2
    you need to give the error, plus more of the code Commented Oct 12, 2018 at 12:54
  • @SureshKumarAriya url working perfect in browser. please share working code if you have done this Commented Oct 12, 2018 at 18:45

1 Answer 1

2

I think the issue you have is not with parsing XML here, but in how you access the URL from within ionic/angular app.

I simplified the code example you have to this:

this.http.get('http://www.gazetaexpress.com/rss/sport/?xml=1').subscribe(data => {
        console.log(data);
      }, error => {
        console.log(error);
    });

Now added it as a method to ionViewDidLoad in this stackblitz: https://stackblitz.com/edit/ionic-4qbega

The error you are getting based on this context is this one:

HttpErrorResponse {headers: {…}, status: 0, statusText: "Unknown Error", url: null…}
error: ProgressEvent
headers: HttpHeaders
message: "Http failure response for (unknown url): 0 Unknown Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "Unknown Error"
url: null
__proto__: HttpErrorResponse

So to fix just data intake you need to do the following:

  • use https URL
  • also use responseType: 'text'

Code:

this.http.get('https://www.gazetaexpress.com/rss/sport/?xml=1', {responseType: 'text'}).subscribe(data => {
        console.log(data);
      }, error => {
        console.log(error);
    });

Now to make this work with your code, it should be something like this:

this.http.get('https://www.gazetaexpress.com/rss/sport/?xml=1', { responseType: 'text'}).subscribe(data => {
    this.xmlItems = data;
    console.log("data:"+data);
    xml2js.parseString(this.xmlItems, function (err, result) {
        console.log(result);
    });
}, error => {
    console.log(error);
});

PS: also check this good article here in case you have more q: https://www.ionicrun.com/transform-xml-to-json-in-ionic-2-with-angular-4-3/

Sign up to request clarification or add additional context in comments.

4 Comments

thanks a lot data is printing now but result showing [object object] on line console.log(result);
I can check it out tomorrow, what version of xml2js are you using
Your code works i just added {responseType: 'text'}) these lines in my code and its working perfect now. Thank u so much Sergey Rudenko for your support, hopefully we keep in touch in future :)
No problem;) glad to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.