Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.84k stars 596 forks source link

Builder splits string into characters, when input has an array of strings #650

Open janakaud opened 1 year ago

janakaud commented 1 year ago

Hello! I went through existing issues but could not find this scenario; feel free to close/link if this has already been asked.

For input:

let json = [
  {
    "arr1": [
      "e11",
      "e12"
    ]
  },
  {
    "arr2": [
      "e21",
      "e22"
    ]
  }
]

and invocation:

const xml2js = require("xml2js");
let xml = new xml2js.Builder({rootName: "arr"}).buildObject(json);
console.log(xml);

I am getting:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<arr>
  <arr1>
    <0>e</0>
    <1>1</1>
    <2>1</2>
    <0>e</0>
    <1>1</1>
    <2>2</2>
    <0>e</0>
    <1>1</1>
    <2>3</2>
  </arr1>
  <arr2>
    <0>e</0>
    <1>2</1>
    <2>1</2>
    <0>e</0>
    <1>2</1>
    <2>2</2>
    <0>e</0>
    <1>2</1>
    <2>3</2>
  </arr2>
</arr>

Is this the expected behavior (I guess not)? If so, is there a setting/parameter which would allow me to get something similar to this instead (where the strings remain intact)?

<arr>
  <arr1>
    <0>e11</0>
    <1>e12</1>
    <2>e13</2>
  </arr1>
  <arr2>
    <0>e21</0>
    <1>e22</1>
    <2>e23</2>
  </arr2>
</arr>

Checked "Options for the Builder class" but could not find a suitable solution.