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

Suggestion: Choose how empty nodes are handled. #683

Open
5 of 6 tasks
Alynntheyelloweyed opened this issue Sep 24, 2024 · 4 comments
Open
5 of 6 tasks

Suggestion: Choose how empty nodes are handled. #683

Alynntheyelloweyed opened this issue Sep 24, 2024 · 4 comments
Labels
Pending Pending to be confirmed by user/author for some check/update/implementation

Comments

@Alynntheyelloweyed
Copy link

  • 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
@amitguptagwl
Copy link
Member

the development of v5 was not completed. so it was deleted as experimental. However, are you looking for empty attribute or tag? updateTag with v4 can be used to set empty attributes. I'll have to check what I did for v5.

@amitguptagwl amitguptagwl added the Pending Pending to be confirmed by user/author for some check/update/implementation label Sep 25, 2024
@Alynntheyelloweyed
Copy link
Author

Alynntheyelloweyed commented Sep 26, 2024

What I need is the following.

the xml

<tagname /> or 
<tagname></tagname> 

to be parsed to

"effects": {}

What I suggested was to give us a option setting to decide if we want empty nodes to be treated as an empty object (my case) an empty array, an empty string value (the v4 way), or "/": true as it is in V5. Heck, you could even give another option to skip it altogether.

@amitguptagwl
Copy link
Member

this is not supported in v4 and current implementation doesn't allow to implement this. I targeted for v5. I would be checking and suggest an example if it is covered.

@Alynntheyelloweyed
Copy link
Author

I've already tried, and as I explained v5 implementation also does not allow for it, hence the suggestion to add it. You can get close, but you cannot actually get there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Pending Pending to be confirmed by user/author for some check/update/implementation
Projects
None yet
Development

No branches or pull requests

2 participants