9

I have this method where I receive an XML response from a remote server and I need to convert the XML to JSON so that Angular 2 can work with the data:

 private extractData(res: Response) {
    let xml = res["_body"]
    console.log(xml);
    var parser = require('xml2json');
    var json = parser.toJson(xml);
    return json
  }

I am trying to use this Node Module: https://www.npmjs.com/package/xml2json

Now this node module is written in javascript (NOT TypeScript) so I'm not sure if I can even use it in an Angular 2 app.

I am getting this compilation error:

ERROR in ./~/isemail/lib/index.js Module not found: Error: Can't resolve 'dns' in '/Users/user/ebayTool/node_modules/isemail/lib' @ ./~/isemail/lib/index.js 5:12-26 @ ./~/joi/lib/string.js @ ./~/joi/lib/index.js @ ./~/xml2json/lib/xml2json.js @ ./~/xml2json/lib/index.js @ ./~/xml2json/index.js @ ./src/app/hero.service.ts @ ./src/app/app.component.ts @ ./src/app/app.module.ts @ ./src/main.ts @ multi webpack-dev-server/client?http://localhost:4200/ ./src/main.ts webpack: Failed to compile.

So my question is how to convert XML to JSON in Angular 2 and how I can properly import xml2json Node Module to be used in my project?

etayluz
  • 15,920
  • 23
  • 106
  • 151

3 Answers3

10

If you use angular-cli to bootstrap your application - it comes already with node module to convert xml.

https://github.com/Leonidas-from-XIV/node-xml2js

So you do not need to add extra modules for this. As it is classic commonJS module - you need use require to import it:

let parseString = require('xml2js').parseString;

So your code can looks like:

let parseString = require('xml2js').parseString;
let xml = "<root>Hello xml2js!</root>"

parseString(xml, function (err, result) {
  console.dir(result);
});

You will receive next output:

enter image description here

In any cases - if you even do not use angular-clior want to use your preffered module to parse xml - use require to load it.

VadimB
  • 5,533
  • 2
  • 34
  • 48
  • Thanks! So I did in fact use the Angular CLI to bootstrap my app. – etayluz Feb 15 '17 at 16:35
  • 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:56
  • 7
    I'm getting similar issues to @dreamdeveloper - the `xml2js` module has dependencies on `events` and `timers`, which are built in NodeJS modules and not available to Angular on the client side. – Chad Sep 22 '17 at 17:27
  • this answer no longer valid as 2023 – Fai Zal Dong May 18 '23 at 08:59
2
function parseXml(xmlStr) {
    var result;
    var parser = require('xml2js');
    parser.Parser().parseString(xmlStr, (e, r) => {result = r});
    return result;
}
pacification
  • 5,838
  • 4
  • 29
  • 51
2

As xml2js uses sax as parser and is a native c module, I would recommend using txml. In your bundle it will only be 4kb and the api is very clean: txml.parse(xmlString)

Disclaimer I am the author of txml.

Tobias Nickel
  • 512
  • 5
  • 7
  • I am trying to use txml for converting from XML to JSON. txml.parse('') is working fine for me. But I want to use "txml.simplify()", as I need simplified version of JSON object. I am using exact syntax as mentioned in the documentation.. i.e. txml.simplify(txml.parse(''); but this is giving me error : Argument of type (string | tNode)[] is not assignable to parameter of type 'tNode[]'. – user3876291 Jul 12 '21 at 11:58