NaturalIntelligence / NaturalIntelligence/fast-xml-builder

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

Open
#12 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
22
Forks
8
PR merge metrics
No merged PRs in 30d

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the StackBlitz reproduction and the XMLBuilder configuration using arrayNodeName and oneListGroup. Compare the generated XML with the expected output; done means each source object is wrapped in its own user element while its addresses remain grouped as repeated address elements.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.