Skip to content

XMLBuilder: Attributes with undefined values produce odd whitespace when used with format: true #585

@ChrisMBarr

Description

@ChrisMBarr
  • Are you running the latest version?
  • Have you included sample input, output, error, and expected output?
  • Have you checked if you are using correct configuration?
  • Did you try online tool?

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?

  • Yes
  • No

Metadata

Metadata

Assignees

No one assigned

    Labels

    PendingPending to be confirmed by user/author for some check/update/implementation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions