node-xml2js icon indicating copy to clipboard operation
node-xml2js copied to clipboard

wrong value assigned to empty tags

Open ex37 opened this issue 11 years ago • 2 comments

Here https://github.com/Leonidas-from-XIV/node-xml2js/blob/master/lib/xml2js.js#L386

obj = _this.options.emptyTag !== '' ? _this.options.emptyTag : emptyStr;

in case we don't pass emptyTag option, or we pass an empty string, emptyStr value is assigned to empty tags. But as we can see here https://github.com/Leonidas-from-XIV/node-xml2js/blob/master/lib/xml2js.js#L371

if (obj[charkey].match(/^\s*$/) && !cdata) {
    emptyStr = obj[charkey];
    delete obj[charkey];
}

in case of newline / space characters emptyStr is set to their initial, non empty string value, no matter if we have trim option passed or no.

ex37 avatar Nov 11 '15 12:11 ex37

EDIT: emptyTag options for parser does nothing for me

Hi, i don't know if my issue is a duplicte of yours, but I'm looking for a solution to:

I have xml with empty tags like so: <tag1> </tag1>

When using xml2js parser, the JSON object I get is: let json = {tag1: [ '\015\n ' ]} when I try to do Object.keys(json.tag1).length I get an error when I should be getting 0.

deeyonn avatar Jun 17 '19 01:06 deeyonn

@ex37 Did you do any workaround for this issue? because I am also facing the same issue while parsing the XML to JSON.

yogeshhub avatar Dec 10 '19 13:12 yogeshhub