1

I am receiving data from an API that uses XML instead of JSON. So far I have the following service for connecting to the API:

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import 'rxjs/add/operator/map';

@Injectable()
export class MyService {
  private searchURL: string = "http://api.testsite.xml";
  constructor(private _http: Http) { }

  getData(){
    return this._http.get(this.searchURL).map(res => res)
  }
}

I subscribe to it in my component like so:

ngOnInit() {
    this._service.getData().subscribe(item=> console.log((<any>item)._body));
  }

This returns a Response object inside which there's a _body property where the whole XML is stored as a string. How do I go about extracting this XML and convert it to JSON? Thanks.

2

1 Answer 1

1

You can use - xml2json.js library. Found this at - Here

var x2js = new X2JS();
var jsonString = x2js.xml_str2json(yourXml);
Sign up to request clarification or add additional context in comments.

Comments

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.