-
-
Notifications
You must be signed in to change notification settings - Fork 331
Description
- Are you running the latest version?
- Have you included sample input, output, error, and expected output?
- Have you checked if you are using correct configuration?
- [] Did you try online tool?
- Have you checked the docs for helpful APIs and examples?
Description
No options set in parser or output builder converts an empty xml node into an empty object. In order to not rewrite my entire project I need the json output for fast-xml-parser to match the output of the module currently in use. Even using v5 did not give me the output desired. I've had to give up utilizing this module.
Input
I've tried every combination of option settings for v5, both parser and object builder and none of them produce the results I want. Based on other threads this should be added in v5. But no option set in either the Parser or Builder does this.
Code
I've tried multiple different options, this one gets me close to my intended output, but not what I expect/want.
import XMLParser from "fast-xml-parser/src/v5/XMLParser.js";
import JsObjOutputBuilder from "fast-xml-parser/src/v5/OutputBuilders/JsObjBuilder.js"
const builder = new JsObjOutputBuilder(
{
attributes: {
prefix: ''
},
nameFor:{
text: '$t'
},
}
)
const parser = new XMLParser (
{
OutputBuilder: builder,
}
);
Output
"effects": {"/": true},
expected data
"effects": {},
In my experimentation I found that onAttribute nearly gets me there but it is flawed.
const builder = new JsObjOutputBuilder({
attributes: {
prefix: '',
entities: true,
},
nameFor:{
text: '$t'
},
onAttribute: (name, value, tagname) => {
if (name == '/') {
name = '';
value='';
//return ''; this was another test
}
return {name, value, tagname};
},
});
making the name and value empty strings gives you effects:{:''}. Return any value by itself gives you effects:{undefined:undefined} while in memory, after you stringify it however, you get {} as expected.
Suggestion
create an onEmptyNode or something to that effect that can be set to 'Object', 'Array', 'String', 'Boolean'
If set to Object it returns {} for empty nodes,
If set to Array it returns [] for empty nodes.
If String it returns "" for empty nodes (current default behavior for 4), or {$text: ""} if alwaysCreateTextNode is set.
If Boolean it returns {"/": true} for empty nodes (current default behavior for v5 JsObjOutputBuilder)
These four look to be covering all the use cases for the other requests I've read here, as well as the two current default behaviors for JsObjectOutputBuilder
I really like this library overall, and it is anywhere between 10 to 35% faster than my current one. But as I can't get empty object output on empty tags it isn't worth switching my entire application's code over to deal with it.
Would you like to work on this issue?
- Yes
- No