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

Wrong behaviour with "arrayNodeName" and "oneListGroup" from XMLBuilder #615

Open ailequal opened 9 months ago

ailequal commented 9 months ago

Description

I need to convert into XML an array of objects, which have same properties stored as array. Ideally I want to set as options for the XMLBuilder "arrayNodeName" (with a custom label) and "oneListGroup" (to "true"). The problem is that these two options don't work really well together, at least as far as I've tested them.

Input

I reproduced the issue here on StackBlitz where I show two different scenarios and how I would expect the data to be actually handled. I'd like each iteration to have its own "user" tag (not the default numeric one, which isn't even allowed as valid xml tag), while also using "oneListGroup" in order to format "addresses" with multiple "address" tags. Instead all the "user" iteration tags are beeing wrapped into a single global "user" tag.

Original source data:

[
  {
    "name": "John",
    "addresses": [
      {
        "address": {
          "street": "123 Fake Street",
          "city": "Faketon",
          "state": "FA",
          "zip": "12345"
        }
      },
      {
        "address": {
          "street": "456 Fake Street",
          "city": "Faketon",
          "state": "FA",
          "zip": "6789"
        }
      }
    ]
  },
  {
    "name": "Jack",
    "addresses": [
      {
        "address": {
          "street": "789 Fake Street",
          "city": "Faketon",
          "state": "FA",
          "zip": "12345"
        }
      },
      {
        "address": {
          "street": "101 Fake Street",
          "city": "Faketon",
          "state": "FA",
          "zip": "6789"
        }
      }
    ]
  }
]

Code

  const builder2 = new XMLBuilder({
    arrayNodeName: 'user',
    oneListGroup: true,
    format: true,
  });

  const test2 = new Promise((resolve) => {
    resolve(`<?xml version="1.0" encoding="UTF-8"?>
<users>
    ${builder2.build(data)}
</users>`);
  });

Output

With both options "arrayNodeName" and "oneListGroup" I got back:

<?xml version="1.0" encoding="UTF-8"?>
<users>
    <user>
  <name>John</name>
  <addresses>
    <address>
      <street>123 Fake Street</street>
      <city>Faketon</city>
      <state>FA</state>
      <zip>12345</zip>
    </address>
    <address>
      <street>456 Fake Street</street>
      <city>Faketon</city>
      <state>FA</state>
      <zip>6789</zip>
    </address>
  </addresses>
  <name>Jack</name>
  <addresses>
    <address>
      <street>789 Fake Street</street>
      <city>Faketon</city>
      <state>FA</state>
      <zip>12345</zip>
    </address>
    <address>
      <street>101 Fake Street</street>
      <city>Faketon</city>
      <state>FA</state>
      <zip>6789</zip>
    </address>
  </addresses>
</user>

</users>

expected data

<?xml version="1.0" encoding="UTF-8"?>
<users>
    <user>
  <name>John</name>
  <addresses>
    <address>
      <street>123 Fake Street</street>
      <city>Faketon</city>
      <state>FA</state>
      <zip>12345</zip>
    </address>
    <address>
      <street>456 Fake Street</street>
      <city>Faketon</city>
      <state>FA</state>
      <zip>6789</zip>
    </address>
  </addresses>
</user>
<user>
  <name>Jack</name>
  <addresses>
    <address>
      <street>789 Fake Street</street>
      <city>Faketon</city>
      <state>FA</state>
      <zip>12345</zip>
    </address>
    <address>
      <street>101 Fake Street</street>
      <city>Faketon</city>
      <state>FA</state>
      <zip>6789</zip>
    </address>
  </addresses>
</user>

</users>

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 9 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.

Logic-Bits commented 7 months ago

oneListGroup + arrayNodeName will also break the attributeNamePrefix feature