xml-js icon indicating copy to clipboard operation
xml-js copied to clipboard

conversion to xml is not working at all

Open sandgupta23 opened this issue 6 years ago • 7 comments

var convert = require('xml-js');

console.log(convert.json2xml(JSON.stringify({"name":'john'}))); console.log(convert.js2xml(JSON.stringify({"name":'john'}))); console.log(convert.json2xml({"name":'john'})); console.log(convert.js2xml({"name":'john'}));

all are printing ""

sandgupta23 avatar May 12 '18 16:05 sandgupta23

please refer this issue for more information https://github.com/nashwaan/xml-js/issues/58

youth7 avatar May 14 '18 09:05 youth7

I saw the code and you should add compact to options if you want a compact conversion otherwise you should add elementskey. I think that the lib should have compact True by default, so that if you don't put options It works. Otherwise It should have a better documentation

euberdeveloper avatar May 16 '18 08:05 euberdeveloper

@sangupta007 As highlighted by @euberdeveloper, the library assumes the input is in non-compact form unless you explicitly pass compact: true option to indicate the input is in compact form.

So, to fix your example, use this code:

var convert = require('xml-js');
console.log(convert.json2xml(JSON.stringify({"name":'john'}), {compact: true}));
// console.log(convert.js2xml(JSON.stringify({"name":'john'}), {compact: true})); // won't work, read below
console.log(convert.json2xml({"name":'john'}, {compact: true}));
console.log(convert.js2xml({"name":'john'}, {compact: true}));

The second case will not work because you are passing JSON object to convert.js2mxl().

nashwaan avatar May 17 '18 06:05 nashwaan

@euberdeveloper I admit the documentation doesn't tell or warn the library works in non-compact form by default. I got tripped into this issue many times myself 😅.

Changing the default behavior to compact form might cause breaking change to existing codes. So I don't think I will go this track.

I will update the documentation to clarify this default. I might also let the library automatically switch the behavior to compact form if the input is in compact form.

nashwaan avatar May 17 '18 06:05 nashwaan

Thanks for reminding me to set compact to true everywhere.

hkchakladar avatar Sep 15 '19 11:09 hkchakladar

Can't the default be set to true? As most json/js objects are of compact form?

Mathijs003 avatar Dec 16 '19 10:12 Mathijs003

I think that if it was done, the module would loose retro-compatibility support. If it was done, the first of the three version-numbers should be incremented

euberdeveloper avatar Dec 16 '19 12:12 euberdeveloper