I have an XML file, the data from which I am trying to upload - first simply with text and then with a JSON object using x2js

clickHandler() { let headers = new Headers(); headers.append('Content-Type', 'text/xml'); let text = ''; this.http.get('./xml/text') .map((res: Response) => res.text()) .subscribe(data => text = data); const x2js = new X2JS(); const jsonObj = x2js.xml2js(xml); let parseString = require('xml2js').parseString; let xml = text; //?? parseString(xml, function (err: any, result: any) { alert(result); } } 

Can you please tell how to accomplish this task?

    1 answer 1

    According to the description of x2js, conversion to json is performed by the xml2json function. But since you receive data by AYAX, then, accordingly, this conversion should be performed when you receive data. That is, inside subscribe

     let text = ''; this.http.get('./xml/text') .map((res: Response) => res.text()) .subscribe(data => { const x2js = new X2JS(); this.jsonObj = x2js.xml2json(data); }); 

    This is how it should look like.