Domanda

I am trying to use xml2js to parse apart xml from rest API return and then turn the modified JSON back into XML to send back. But I can't seem to get xml2js's default builder example from their GitHub to work. Any recommendation on what's wrong here or another node package to use to accomplish this?

var fs = require('fs'),
    xml2js = require('xml2js');

var obj = { name: "Super", Surname: "Man", age: 23};

var builder = new xml2js.Builder();
var xml = builder.buildObject(obj);

The Error I am getting back is:

var builder = new xml2js.Builder();
          ^
TypeError: undefined is not a function
    at Object.<anonymous> (/path/test.js:6:15)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:312:12)
    at Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:901:3
È stato utile?

Soluzione

As I said on GitHub:

This is because the XML Builder hasn't been published to npm yet. If you wish to use it, you should download the repo from GitHub and use it as such. (don't forget to run npm install when doing so)

Altri suggerimenti

Agree, would be helpful if that was in the notes as wasted a little bit of time figuring out what was wrong and ended up here.

To install via NPM from master just use

npm install https://github.com/Leonidas-from-XIV/node-xml2js/archive/master.tar.gz

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top