I wanted to convert the XML I received from Web API response to JSON in Angular 2. The application is developed in Nativescript. Not able to find a solution for this.
Asked
Active
Viewed 2.6k times
8
-
Did you tried this one: https://www.npmjs.com/package/nativescript-xml2js – galvan Mar 16 '17 at 15:30
-
Duplicate of http://stackoverflow.com/questions/36368405/how-to-parse-xml-in-angular-2. – Gili Yaniv Mar 16 '17 at 15:52
-
Possible duplicate of [How to parse xml in Angular 2](http://stackoverflow.com/questions/36368405/how-to-parse-xml-in-angular-2) – AT82 Mar 16 '17 at 19:34
-
As informed by Nativescript team and as experimented, none of the above said modules seems to be working with nativescript angular 2. So is there any other alternatives which can be used with nativescript angular 2. – dreamdeveloper Mar 21 '17 at 11:30
-
Weird, so you looked up support for the nativescript-xml2js and it said it it does not work for nativescript? – wuno Mar 21 '17 at 17:34
-
nativescript-xml2js wont work with nativescript. They said like the author of the plugin is not an active member and this is not something which is supported in nativescript. Just the name is confusing. Nativescript has not yet provided a proper solution for this. – dreamdeveloper Mar 22 '17 at 06:22
3 Answers
7
I found an amazing package to make this very simple.
For me on I am doing it in an angular 2 application but on the node side.
npm install xml2js --save
It is literally as simple as passing the xml like this,
var parseString = require('xml2js').parseString;
var xml = "<root>Hello xml2js!</root>"
parseString(xml, function (err, result) {
console.dir(result);
});
In my app I had an xml file and used it like this,
var fs = require('fs');
var parseString = require('xml2js').parseString;
function requestCreditReport(callback) {
fs.readFile('./credit-api/response.xml', 'utf8', function (err,data) {
if (err) return callback(err);
parseString(data, callback);
});
}
I hope this helps.

wuno
- 9,547
- 19
- 96
- 180
-
Somehow i am not able see the log inside the parseAtring function. Soap envelop is my response from web api. – dreamdeveloper Mar 16 '17 at 16:52
-
So you are saying that you are trying to console result and it is not working? would you please console.log outside of the function a test. To make sure we are running the file. – wuno Mar 16 '17 at 16:58
-
The console.dir is not working and i am not sure how to get the result out of it. var parseString = require('xml2js').parseString; var xml = "
Hello xml2js! " parseString(xml, function (err, result) { console.dir(result); }); – dreamdeveloper Mar 16 '17 at 18:21 -
try this, var parseString = require('xml2js').parseString; var xml = "
Hello xml2js! "; var jString = parseString(xml); console.log(jString); – wuno Mar 16 '17 at 18:59 -
I am getting the below error when i try to use the xml2js module. 03-17 02:37:48.190: V/JS(7130): error ::: Error: com.tns.NativeScriptException: Failed to find module: "events", relative to: app/tns_modules/ 03-17 02:37:48.190: V/JS(7130): com.tns.Module.resolvePathHelper(Module.java:159) 03-17 02:37:48.190: V/JS(7130): com.tns.Module.resolvePath(Module.java:60) 03-17 02:37:48.190: V/JS(7130): com.tns.Runtime.callJSMethodNative(Native Method) – dreamdeveloper Mar 17 '17 at 06:51
-
I do not know for sure if that will work for nativescript. But here is an event module. This could have something else to do with your app https://www.npmjs.com/package/events – wuno Mar 17 '17 at 06:56
-
Wait see if this makes a difference https://www.npmjs.com/package/nativescript-xml2js – wuno Mar 17 '17 at 06:57
-
-
using nativescript-xml2js.I am getting the below exception.'Error: com.tns.NativeScriptException:Failed to find module: "xml2js", relative to: app/tns_modules/' the i am using is below, getJsonfromXml() { try{ let parseString = require('xml2js').parseString; let xml = "
Hello xml2js! "; parseString(xml, function (err, result) { if (err) throw err; console.dir(result); }); } catch (e){ console.log("error :: "+e); } } – dreamdeveloper Mar 17 '17 at 08:54 -
As informed by Nativescript team and as experimented, none of the above said modules seems to be working with nativescript angular 2. So is there any other alternatives which can be used with nativescript angular 2. – dreamdeveloper Mar 21 '17 at 11:30
6
This is if you are doing a POST and getting back XML response using Angular 2: Use xml2js - https://www.npmjs.com/package/xml2js
- npm install xml2js --save
import in service file as:
import * as xml2js from 'xml2js';
Code:
let formdata = new URLSearchParams(); formdata.set('username','username'); formdata.set('pw','pw'); let headers = new Headers({'Content-Type': 'application/x-www-form-urlencoded' }); let options = new RequestOptions({ headers: headers, method: RequestMethod.Post}); postData () { this.http.post(this._yourUrl, formdata.toString(), options) //convert to JSON here .map(res => { xml2js.parseString( res.text(), function (err, result) { console.dir(result); // Prints JSON object! }); }) .subscribe(data => { console.log(data); }); }
0
function parseXml(xmlStr) {
var result;
var parser = require('xml2js');
parser.Parser().parseString(xmlStr, (e, r) => {result = r});
return result;
}

Pankaj kumar
- 31
- 4
-
5Add some more context to your code to help future readers better understand its purpose. – Grant Miller Jul 23 '18 at 02:31