NaturalIntelligence / fast-xml-parser

Validate XML, Parse XML and Build XML rapidly without C/C++ based libraries and no callback.
https://naturalintelligence.github.io/fast-xml-parser/
MIT License
2.53k stars 303 forks source link

XMLBuilder Incorrectly Serializes String "true" as Boolean Attribute #656

Closed Undefined01 closed 4 months ago

Undefined01 commented 4 months ago

Description

When using XMLBuilder to serialize attributes with the string value "true", they are incorrectly serialized as Boolean attributes, regardless of the Allow Boolean Attributes option's state. This results in an output where the attribute appears without an explicit value, which differs from the expected behavior where the attribute should explicitly show "true". ### Input
{
    "test": {
        "@_property1": "true",
        "@_property2": "false"
    }
}

Code

const fxp = require('fast-xml-parser');
const opts = {
    ignoreAttributes: false
};
const xmlObj = {
    test: {
        "@_property1": "true",
        "@_property2": "false"
    }
};
const builder = new fxp.XMLBuilder(opts);
console.log(builder.build(xmlObj));

Output

<?xml version="1.0"?>
<test property1 property2="false"></test>

expected data

<?xml version="1.0"?>
<test property1="true" property2="false"></test>

Would you like to work on this issue?

Bookmark this repository for further updates. Visit SoloThought to know about recent features.

github-actions[bot] commented 4 months ago

We're glad you find this project helpful. We'll try to address this issue ASAP. You can vist https://solothought.com to know recent features. Don't forget to star this repo.

amitguptagwl commented 4 months ago

Please read https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/docs/v4/3.XMLBuilder.md

Undefined01 commented 4 months ago

Setting suppressBooleanAttributes to false solves this problem.

I think it is kind of unintuitive. It's odd that {"@_Property": "true"} gets treated as a boolean attribute and suppressed, yet {"@_Property": false} and {"@_Property": "false"} are not affected in the same way. Besides, I would expect they will not be regarded as boolean attributes and suppressed after setting allowBooleanAttributes to false.

wodepig commented 1 month ago

设置为 可解决此问题。suppressBooleanAttributes``false

我认为这有点不直观。奇怪的是,它被视为 boolean 属性并被抑制,但 并且没有以相同的方式受到影响。此外,我希望它们不会被视为布尔属性并在设置为 false 后被抑制。{"@_Property": "true"}``{"@_Property": false}``{"@_Property": "false"}``allowBooleanAttributes

is worked ,thanks