Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.84k stars 598 forks source link

Remove Root node #611

Open assaad97 opened 3 years ago

assaad97 commented 3 years ago

hi, im converting a json object to a xml file, using builder class, and i want to deny the root node, meaning, to convert the json object to an xml without adding the root node, i tried the option explicitRoot to false and true without any change, if anyone knows how to do it, i would appreciate the help

carlosconnected commented 1 year ago

I have the same question

afarah1 commented 1 year ago

If the object has only one key, it will be used as the root, otherwise, a root node will be created. See:

https://github.com/Leonidas-from-XIV/node-xml2js/blob/master/src/builder.coffee#L34

Not sure if it helps, but in my case I had

builder.buildObject({'foo': {$: {myAttr: 'myValue'}}, bar: 'bar', baz: 'baz'})
'<root><foo myAttr="myValue"/><bar>bar</bar><baz>baz</baz></root>'

When what I really wanted was

builder.buildObject({'foo': {$: {myAttr: 'myValue'}, bar: 'bar', baz: 'baz'}})
'<foo myAttr="myValue"><bar>bar</bar><baz>baz</baz></foo>'
windstar20 commented 1 year ago

Maybe your XML structure can have more than one parent element. Example `

`

You have to be only one "parent element" Like this... `

`