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.

blankface
  • 5,757
  • 19
  • 67
  • 114

1 Answers1

1

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

var x2js = new X2JS();
var jsonString = x2js.xml_str2json(yourXml);