fast-xml-parser
fast-xml-parser copied to clipboard
Empty node strings
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 "" .
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.
@Marius8881 Can you please give some example? I'm working on fxp v4. I can probably consider new requirements in that.
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.
Do you expect overlays = ' '
in case of <overlays> <overlays/>
?
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.
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.
closing as no update. reopen it if issue still exists
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 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.
+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.
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.
Let's assume 2 situation <a><![CDATA[text]]></a>
and <a><tag>text</tag></a>
. Should we call tagValueProcessor
for <a>
?
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.
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?
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?
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
check if updateTag
can help.
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);
},
});
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")
Please check v. It's experimental but should help. I've added basic documentation.