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

XML sequential order is not respected #13

Open
pmaziere opened this issue Apr 12, 2017 · 2 comments
Open

XML sequential order is not respected #13

pmaziere opened this issue Apr 12, 2017 · 2 comments

Comments

@pmaziere
Copy link

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

@jbmonroe
Copy link

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.

@javadev
Copy link

javadev commented Nov 7, 2018

@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"
      }
    ]
  }
}

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

3 participants