xml2json icon indicating copy to clipboard operation
xml2json copied to clipboard

XML sequential order is not respected

Open pmaziere opened this issue 7 years ago • 2 comments

Hi,

when a xml document contains several instances of 2 different kinds of XML tags , xml2json regroups them in 2 different json arrays, ignoring the original sequence of the xml document. Example:

<xml>
<tag1 id="1"/>
<tag1 id="2"/>
<tag2 id="1"/>
<tag2 id="2"/>
<tag1 id="3"/>
</xml>

gives this result:

{"xml":{
"tag1":[{"@id":"1"},{"@id":"2"},{"@id":"3"}],
"tag2":[{"@id":"1"},{"@id":"2"}]
}

which is understandable because the standard says that the names within an object SHOULD be unique.

To conserve this sequential information in the resulting json, I was expecting something like:

{"xml":{
"tag1[0]":[{"@id":"1"},{"@id":"2"}],
"tag2":[{"@id":"1"},{"@id":"2"}],
"tag1[1]":{"@id":"3"}
}

But someone may have a better approach to propose.

Could it be considered as an optional feature ?

thanks

pmaziere avatar Apr 12 '17 22:04 pmaziere

I think I have to respectfully disagree here. The standard says that you can't depend on the order of keys in a JavaScript object (which is what you end up with in the interpreter), and tag1[0] and tag1[1] are distinct identifiers. The XML clearly provides that the two arrays are tag1 and tag2. As you suggested it be done, there would be extra work required on the part of the coder after the JSON.parse to make sense of the data. In my opinion that is unexpected and in general, unexpectedness is not part of a good design.

To look at it a different way: an XSD can require a specific ordering in XML, but JSON is schema-less.

jbmonroe avatar Jun 27 '18 20:06 jbmonroe

@pmaziere

<xml>
<tag1 id="1"/>
<tag1 id="2"/>
<tag2 id="1"/>
<tag2 id="2"/>
<tag1 id="3"/>
</xml>

may be converted to this json

{
  "xml": {
    "tag1": [
      {
        "-id": "1",
        "-self-closing": "true"
      },
      {
        "-id": "2",
        "-self-closing": "true"
      },
      {
        "#item": {
          "tag2": [
            {
              "-id": "1",
              "-self-closing": "true"
            },
            {
              "-id": "2",
              "-self-closing": "true"
            }
          ]
        }
      },
      {
        "-id": "3",
        "-self-closing": "true"
      }
    ]
  }
}

javadev avatar Nov 07 '18 04:11 javadev