Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
4 of 6 tasks
ailequal opened this issue Oct 2, 2023 · 2 comments
Open
4 of 6 tasks

Comments

@ailequal
Copy link

ailequal commented Oct 2, 2023

  • 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

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?

  • Yes
  • No

Bookmark this repository for further updates. Visit SoloThought to know about recent features.

@github-actions
Copy link

github-actions bot commented Oct 2, 2023

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
Copy link

oneListGroup + arrayNodeName will also break the attributeNamePrefix feature

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants