Leonidas-from-XIV / node-xml2js

XML to JavaScript object converter.
MIT License
4.87k stars 601 forks source link

Getting two elements with one child instead of one element with two children #585

Closed dobesv closed 3 years ago

dobesv commented 3 years ago

I wanted to have an element with two child elements in it, but I got two elements each with one child.

> console.log(new xml2js.Builder().buildObject({ aaa: { header: [{rhi: [{v: ['1']}, {mid: ['123']}]}]}}))
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<aaa>
  <header>
    <rhi>
      <v>1</v>
    </rhi>
    <rhi>
      <mid>123</mid>
    </rhi>
  </header>
</aaa>

I expected to get:

> console.log(new xml2js.Builder().buildObject({ aaa: { header: [{rhi: [{v: ['1']}, {mid: ['123']}]}]}}))
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<aaa>
  <header>
    <rhi>
      <v>1</v>
      <mid>123</mid>
    </rhi>
  </header>
</aaa>

Interestingly, when I pass this through parse it merges the two elements back together:

> new xml2js.Parser({explicitArray: true}).parseStringPromise(s).then(d => JSON.stringify(d, null, 2)).then(console.log)
Promise { <pending> }
> {
  "aaa": {
    "header": [
      {
        "rhi": [
          {
            "v": [
              "1"
            ]
          },
          {
            "mid": [
              "123"
            ]
          }
        ]
      }
    ]
  }
}
dobesv commented 3 years ago

Ah ... hmm I think I was just really confused about how the javascript objects are formatted. The above is probably the correct behavior.