fast-xml-parser icon indicating copy to clipboard operation
fast-xml-parser copied to clipboard

Empty node strings

Open Marius8881 opened this issue 5 years ago • 20 comments

Hi, Is it possible to select certain empty node values, "" , and change them to different types, ex, array ? I do not seem to see the option for this., tried it with tagValueProcessor , but it does not see empty "" .

Marius8881 avatar Feb 23 '20 16:02 Marius8881

I'm glad you find this repository helpful. I'll try to address your issue ASAP. You can watch the repo for new changes or star it.

github-actions[bot] avatar Feb 23 '20 16:02 github-actions[bot]

@Marius8881 Can you please give some example? I'm working on fxp v4. I can probably consider new requirements in that.

amitguptagwl avatar Feb 26 '20 00:02 amitguptagwl

 Example:
    <x>0</x>
    <y>44</y>
    <width>2</width>
    <minWidth>1</minWidth>
    <maxWidth>12</maxWidth>
    <height>4</height>
    <minHeight>4</minHeight>
    <maxHeight>48</maxHeight>
    <overlays/>
    <frame>
      <static>true</static>
      <nodePath/>
      <color>transparent</color>
    </frame>
 

<overlays/> is coming as empty, and I can not see the name of the attribute, because of it, even if its set as <overlays> <overlays/> , same for arrays.

Marius8881 avatar Mar 04 '20 08:03 Marius8881

Do you expect overlays = ' ' in case of <overlays> <overlays/> ?

amitguptagwl avatar Mar 06 '20 11:03 amitguptagwl

Yes, unfortunately we deal with binary data, and need to use xml, and have issues with the types changing, and we needed to change the type value of a node to what we need, that is why is favorable to see them all, even if empty.

Marius8881 avatar Mar 06 '20 11:03 Marius8881

Try trimValues: false. But it may add "#text": "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n", to the output.

amitguptagwl avatar Mar 14 '20 09:03 amitguptagwl

closing as no update. reopen it if issue still exists

amitguptagwl avatar Nov 17 '21 02:11 amitguptagwl

In my example, I'm parsing this:

<companies>
  <company></company>
</companies>

which parses into this:

companies: {
  company: ''
},

vs.

companies: {
  company: {}
},

Is there an option to control this?

fpedroza avatar Jun 08 '22 20:06 fpedroza

@fpedroza Currently no. But you can create another issue for a feature request. If there are more users who are looking for the same or similar behavior then we can provide some common solution.

amitguptagwl avatar Jun 09 '22 00:06 amitguptagwl

+1 for this issue. I would like to treat an empty tag as a null value, and I cannot process it with the tagValueProcessor because it skips empty nodes.

bobspace avatar Jun 15 '22 23:06 bobspace

in my case, if specific empty tag exists, I need to replace its value with boolean, but currently, it is impossible due to tagValueProcessor skips empty tags.

dobeerman avatar Jul 24 '22 19:07 dobeerman

Let's assume 2 situation <a><![CDATA[text]]></a> and <a><tag>text</tag></a>. Should we call tagValueProcessor for <a>?

amitguptagwl avatar Oct 07 '22 06:10 amitguptagwl

I've created a branch "emptyNode_v2" for this change. But not publishing them due to a few pending decisions like when not to call tagValueProcessor.

amitguptagwl avatar Oct 07 '22 07:10 amitguptagwl

We have absolutely the same case when we need to replace empty string with empty object.

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<PutDataRequest>
    <Commands>
      <PartialRetransmissions>
      </PartialRetransmissions>
      <RetransmissionsTracking>
          <Retransmission Begin="2021-10-29T20:17:10.786+00:00" End="2021-10-29T22:23:01.220+00:00" />
          <Retransmission Begin="2021-11-02T16:04:04.851+00:00" End="2021-11-02T18:23:18.939+00:00" />
      </RetransmissionsTracking>
    </Commands>
</PutDataRequest>

The returned Commands object as follow

{
    "PartialRetransmissions": "",
    "RetransmissionsTracking": {
      "Retransmission": [
        {
          "@_Begin": "2021-10-29T20:17:10.786+00:00",
          "@_End": "2021-10-29T22:23:01.220+00:00"
        },
        {
          "@_Begin": "2021-11-02T16:04:04.851+00:00",
          "@_End": "2021-11-02T18:23:18.939+00:00"
        }
      ]
    }
  }

We simply want to replace, for instance, "PartialRetransmissions": "", with "PartialRetransmissions": {},, as we expect to have an object here, by validating jsonPath and tagValue. I tried to use tagValueProcessor as I expected it will help us, but, unfortunatelly, the tagValueProcessor not calling by parser at all.

  tagValueProcessor: (tagName, tagValue, jPath, hasAttributes, isLeafNode) => {
    if (alwaysArray.includes(jsonPath) && !tagValue) return {};
    return tagValue;
  },

Am I do something wrong?

dobeerman avatar Jan 28 '23 11:01 dobeerman

I too have same issue where tagValueProcessor does not execute for empty tags, I want to change the structure of some empty tag instead of default empty string.

eg: <User />

`output { user: "", }

instead { user: {}, }`

can we execute tagValueProcessor for empty tags?

htrap007 avatar Apr 06 '23 07:04 htrap007

I'm having a similar requirement where I would rather return an empty {} or [] instead of the default "" returned by the library. Wouldn't it be a first simple step to either execute tagValueProcessor for empty strings? Maybe an additional setting to activate this would be good if there are performance implications.

Thanks

dan852 avatar May 18 '23 02:05 dan852

check if updateTag can help.

amitguptagwl avatar May 22 '23 15:05 amitguptagwl

I also did not find a solution for this apart from manual post-processing.

I think @fpedroza example illustrates this best - with:

<companies>
  <company></company>
</companies>

there should be an option to parse it to:

{
  "companies": {
    "company": {}
  }
}

Otherwise, company is once an object (if it has any child tags), once a string (like in above XML).

What would work could be isObject() function, with behavior analogous to isArray():

const alwaysObject = ["companies.company"];
const parser = new XMLParser({
	isObject: (name, jPath) => {
		return alwaysObject.includes(jPath);
	},
});

m-radzikowski avatar Jun 16 '23 15:06 m-radzikowski

Any updates? I also need this feature. In my case, I need to convert empty nodes to null if they have a certain attributes (nil="true")

arshia-gh avatar Mar 23 '24 14:03 arshia-gh

Please check v. It's experimental but should help. I've added basic documentation.

amitguptagwl avatar Mar 24 '24 09:03 amitguptagwl