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: Attributes with `undefined` values produce odd whitespace when used with `format: true` #585

Closed ChrisMBarr closed 1 year ago

ChrisMBarr commented 1 year ago

Description

Using XMLBuilder when the format: true option is on and an array contains a property with undefined as the value it produces odd whitespace formatting. The text is not on the same line as the opening XML tag, and there are several spaces added before the closing tag.

The XML output is correct in how it creates the tags and their attributes, but the whitespace is just all wrong.

Code

const xmlObj = {
  "list": {
    "item":[
      "one",
      {"#text": "two"},
      {"#text": "three", "@_attr": undefined},
      {"#text": "two",   "@_attr": "foo"}
    ]
  }
};
const opts = {
  ignoreAttributes: false,
  format: true
};
const builder = new fxp.XMLBuilder(opts);
console.log(builder.build(xmlObj));

Actual Output

<list>
  <item>one</item>
  <item>two</item>
  <item>
three  </item>
  <item attr="foo">two</item>
</list>

Expected Output

<list>
  <item>one</item>
  <item>two</item>
  <item>three</item>
  <item attr="foo">two</item>
</list>

What I am working on has optional object properties, so I just pass the data to the property as is. Here is my real use case in my TypeScript project

interface IObj {
  value: string;
  part?: string;
}

function getXmlNodes(dataArr: IObj[]){
  const xmlObjs = [];

  for(let item of dataArr){
    xmlObjs.push({
      '#text': item.value,
      '@_part': item.part,
    });
  }

  return xmlObjs;
}

Would you like to work on this issue?

github-actions[bot] commented 1 year 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.

cecia234 commented 1 year ago

Hi @amitguptagwl, I found the same odd behaviour. Moreover, there is another "odd" case with formatting enabled when attributes are specified as null.

Using the same structure as the one of @FiniteLooper, with the starting object as follows

const xmlObj = {
  "list": {
    "item":[
      "one",
      {"#text": "two"},
      {"#text": "three", "@_attr": null},
      {"#text": "two",   "@_attr": "foo"}
    ]
  }
};

the final output is

<list>
  <item>one</item>
  <item>
three    <@_attr/>
  </item>
  <item attr="foo">two</item>
</list>

If this and the former are unexpected behaviours, I could fix them! I already looked at the code and it shouldn't be that hard.

In that case, I think that the expected output should also be discussed (empty string in the attribute value, no attribute at all, etc.)

amitguptagwl commented 1 year ago

Hi @cecia234, since null and undefined doesn't represent any value, it's better if we skip that attribute from the output.