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

remove _attributes object maintaining its value

Open emavitta opened this issue 5 years ago • 2 comments

is it possibile to format the js object in a way that the _attributes object has only its value? from something like this

{ _attributes: { name: 'titolo', description: 'il titolo della sezione' }}

to something like this

{ name: 'titolo', description: 'il titolo della sezione' }

emavitta avatar Jun 12 '20 10:06 emavitta

I can say that based off the README, no. Those keys are reserved for elements ().

amcsi avatar Feb 18 '21 08:02 amcsi

I actually tried to do that too but could not do it via the library, therefore i used post-cleaning of the result like below.

//parse xml-js result (which is a json string), so we can convert it from json string to json object var convertedXml = JSON.parse(result); //use traverse function to add the _attributes' properties into the _attributes Object itself and then delete the _attributes Object convertedXml = traverse_it(convertedXml); function traverse_it(obj: any) { for (var prop in obj) { if (typeof obj[prop] == 'object') { if (obj[prop]._attributes) { Object.assign(obj[prop], obj[prop]._attributes); delete obj[prop]._attributes; } traverse_it(obj[prop]); } } return obj; }

If you need the result as a string, then you can use JSON.stringify(convertedXml);

AnisaAl avatar Mar 26 '21 15:03 AnisaAl