Hey I’m trying to parse a javascript object to xml string in node.js app. I’m using the npm package json2xml
like this
json2xml(jsonObject, { header: true });
my issue is that when there’s an array object it will parse it like this:
root: {
foo: [
{
bar: 'a',
},
{
bar: 'b',
},
],
};
<root>
<foo>
<bar>a</bar>
<bar>a</bar>
</foo>
and not like this:
<root>
<foo>
<bar>a</bar>
</foo>
<foo>
<bar>b</bar>
</foo>
</root>
as I wish it did. Do you have any suggestions or any other libraries I can use to fix this issue?
I’ve tried couple of other libraries: jstoxml
, @javadev/xmltojson
, fast-xml-parser
, that didn’t fix the issue. I’m thinking to write my own jsToXml parser xD
4
Answers
Ok suppose I have a little more complex object like this:
I haven't found a way how to convert the js object to xml to this:
instead I always get this:
The object should be composed like that:
it’s not possible to convert your more complex example as you want, unless you add a key in ‘params’, but it’s not the same thing:
exactly it’s not the same thing, then I’ll have this:
so I’m not sure, if what I want is possible with this library. I just wanted to make sure if there’s any solution. If not it’s okay and I can find some other way.