-
Notifications
You must be signed in to change notification settings - Fork 606
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
Properties as Array #304
Comments
|
@Laro88 What |
I was thinking while writing, xml sample provided now:
Here METER_ID is an array which is erroneous:
I have cut of the full xml, it is valid, full files available upon request. The data is coming from a "concentrator" grabbing date from some wireless sensors and relaying them to my Node.JS code. I found this issue when adding mergeAttrs : true - it would make the code more readable, but I am hitting this issue with properties becoming arrays. |
I cant reproduce the behavior in a unit test. I will fault back to the $ and _ semantics just to be safe. |
I have this issue too. It happens when |
#309 Addresses this! |
I think this issue should be closed as "Won't fix". It makes sense to have attribute values in arrays when Even with Example: const xml2js = require('xml2js');
let parser = new xml2js.Parser({
mergeAttrs: true,
explicitArray: false,
});
let xml = `<x y='attVal'><y>eleVal</y></x>`;
parser.parseString(xml, (err, res) => {
console.log(res);
}); Output: { x: { y: [ 'attVal', 'eleVal' ] } } |
@jcsahnwaldt In what way does that make sense? If I have a node with attributes on it:
I expect the JSON representation to have those properties as primitive values and not an array with one element. With nodes, yes, However, with attributes, they are just strings. Why should those be parsed as an array containing only that value? |
<foo bar="bar"><bar>baz</bar></foo> See #101 for details. |
@jcsahnwaldt Although that is a valid point in the wild west world of XML, is that really a realistic schema? Why would there ever be an attribute and a child node named the exact same thing? I believe we should if nothing else have an additional option to disregard attributes when combining I am simultaneously replying to your comment on #309 for more context for anybody who may come across this. |
I'll reply at #309. |
@jcsahnwaldt I put up a PR with a new option to allow users to fix this: #516. Please take a look at it and let me know what you think. |
@jcsahnwaldt Did you get a chance to review the PR? We could use this change in our project, so I'd like to either merge the changes sooner than later or find out what should be done differently. |
@IRCraziestTaxi I'm sorry, I didn't have time yet, and I probably won't have time for another few weeks or so. Also, I'm not a committer on this project. I created a few PRs about a year ago, but I haven't worked with node-xml2js since. It looks like @Leonidas-from-XIV doesn't have much time to look into PRs either (I'm not blaming him). I guess your best option may be to create a fork, implement the changes you need, and use your own (patched) version of node-xml2js. |
Update: For anyone coming across this issue and wanting the fix, while we're waiting for my PR to be reviewed, place this in the "xml2js": "https://github.com/IRCraziestTaxi/node-xml2js/tarball/304-properties-as-array-fix" Usage: xmlParseOptions: {
ignoreAttrs: false,
mergeAttrs: true,
mergeAttrsArray: false
} |
I am in full agreement with @jcsahnwaldt here. |
In the old version, the XML properties they were not being treated as an array. With the new version behavior changed all properties are now array.
I think the previous behavior was correct.
Old version:
if (_this.options.mergeAttrs) {
obj[processedKey] = newValue;
}
New version change:
if (_this.options.mergeAttrs) {
_this.assignOrPush(obj, processedKey, newValue);
}
The text was updated successfully, but these errors were encountered: