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.43k stars 297 forks source link

Not getting attribute value using XMLBuilder.build function #651

Closed Faizan2030 closed 1 month ago

Faizan2030 commented 2 months ago

Description

I need to include xml attribute, like this : <internal-name tokenized="true">test test &gt; 14</internal-name>

Input

Code

defaultXmlOptions: {
      ignoreAttributes: false,
      attributeNamePrefix: "@_",
      indentBy: "  ",
      textNodeName: "#text",
      format: true,
  },
var testJson = { component: {
      "internal-name": {
          "#text": "test test > 14",
          "@_tokenized": true
      }
  }}

Here is the code that converts:

var parser = new XMLBuilder(CRUDService.defaultXmlOptions);
var xml = parser.build(testJson);

That attribute should have ="true"

Output

<component>
  <internal-name tokenized>test test &gt; 14</internal-name>
</component>

expected data

<component>
  <internal-name tokenized="true">test test &gt; 14</internal-name>
</component>

Would you like to work on this issue?

I posted the question here as well https://stackoverflow.com/questions/78354831/how-to-include-attribute-value-while-building-xml-using-fast-xml-parser

github-actions[bot] commented 2 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 2 months ago

Can you please share the intermediate json object?

Faizan2030 commented 2 months ago
defaultXmlOptions: {
      ignoreAttributes: false,
      attributeNamePrefix: "@_",
      indentBy: "  ",
      textNodeName: "#text",
      format: true,
  },
var testJson = { component: {
      "internal-name": {
          "#text": "test test > 14",
          "@_tokenized": true
      }
  }}
amitguptagwl commented 2 months ago

got your problem. "@_tokenized": true this is set as boolean. Can you try to set it as string?

MarcoPolo-PasTonMolo commented 2 months ago

Just tried this setting it as a string changes nothing. What you can do however is to add this property to your options and the original should work suppressBooleanAttributes: false

MarcoPolo-PasTonMolo commented 2 months ago

Also see #611

amitguptagwl commented 1 month ago

closing the issue. feel free to reopen if still exist. Please also check for v5