The https://github.com/buglabs/node-xml2json/pull/180 maintainers (buglabs
, no e-mail, no GitHub) are not responsive. So, I created a small fix, that works perfectly, it is named as p3x-xml2json
. It is the same as the original, just works with NodeJs 12.
I am open to merge pull requests. I maintain. Please free, to contact me.
Besides, I have upgraded all packages to the latest versions.
It does not parse the following elements:
This module uses node-expat which will require extra steps if you want to get it installed on Windows. Please refer to its documentation .
$ npm install p3x-xml2json
var parser = require('p3x-xml2json');
var xml = "<foo attr=\"value\">bar</foo>";
console.log("input -> %s", xml)
// xml to json
var json = parser.toJson(xml);
console.log("to json -> %s", json);
// json to xml
var xml = parser.toXml(json);
console.log("back to xml -> %s", xml)
parser.toJson(xml, options);
parser.toXml(json);
toJson
Default values:
var options = {
object: false,
reversible: false,
coerce: false,
sanitize: true,
trim: true,
arrayNotation: false
alternateTextNode: false
};
var chars = {
'<': '<',
'>': '>',
'(': '(',
')': ')',
'#': '#',
'&': '&',
'"': '"',
"'": '''
};
toXml
Default values:
var options = {
sanitize: false,
ignoreNull: false
};
sanitize: false
is the default option to behave like previous versions(*) xml2json tranforms CDATA content to JSON, but it doesn't generate a reversible structure.