Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order of elements is being destroyed #499

Open
philiprbrenan opened this issue Feb 12, 2019 · 6 comments
Open

Order of elements is being destroyed #499

philiprbrenan opened this issue Feb 12, 2019 · 6 comments

Comments

@philiprbrenan
Copy link

Parsing the following Xml:

"<a id='1'><b id='b1'/><c id='c1'/><b id='b2'/></a>"

yields:
{"a":{"$":{"id":"1"},"b":[{"$":{"id":"b1"}},{"$":{"id":"b2"}}],"c":[{"$":{"id":"c1"}}]}}

which destroys the fact that c1 occurs before b2. Sample test file:

const parseString = require('xml2js').parseString;

const xml = "<a id='1'><b id='b1'/><c id='c1'/><b id='b2'/></a>"

parseString(xml, function (err, result)
 {process.stderr.write(JSON.stringify(result)+"\n");
 });
@UnbearableBear
Copy link

UnbearableBear commented Mar 1, 2019

Yeah they have no fix for that except mixing three options which are:

explicitChildren: true,
preserveChildrenOrder: true,
charsAsChildren: true

And honestly, the output is twisted as hell with all the data duplicated. I cannot afford that, I'm switching to another library.

@bluenote10
Copy link

@UnbearableBear May I ask which one? I also can't seem to get order preserving roundtrips out of this lib...

@UnbearableBear
Copy link

@bluenote10 I use this one which works fine to me but I'm not sure it is maintained anymore https://github.com/nashwaan/xml-js

@alcalyn
Copy link

alcalyn commented May 8, 2020

Thank you for the alternative.

I tried preserveChildrenOrder: true, then explicitChildren: true, preserveChildrenOrder: true, and it was not working, I couldn't guess I need these 3 options.

I think it's not intuitive to just keep the xml content order, and yes the result is too much duplicated.

@marvinirwin
Copy link

Ran into this issue and switched to xml-js to fix.

@eMahtab
Copy link

eMahtab commented Jul 4, 2022

Faced the same issue, unfortunately there was no way to get the exact order.
explicitChildren: true, preserveChildrenOrder: true, charsAsChildren: true
Even if you use above three options, you will get a lot of duplicated data.
I switched to https://github.com/nashwaan/xml-js that worked for my requirements. It preserves the order.

Also if your input xml is deeply nested, you might find the https://marketplace.visualstudio.com/items?itemName=nidu.copy-json-path plugin useful (I guess similar plugins are available for other IDE's as well). You can easily navigate the output JSON using this plugin. This saved a lot of time for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants