Skip to content

How to convert a node of xml to json array #595

Closed

Description

@relax-admin

This is achieved in python, is it possible to add similar parameters in java

In the following example, interfaces are parsed into an array as expected

import json

import xmltodict

t1 = '''
<servers>
  <server>
    <name>host1</name>
    <os>Linux</os>
    <interfaces>
      <interface>
        <name>em0</name>
        <ip_address>10.0.0.1</ip_address>
      </interface>
    </interfaces>
  </server>
</servers>
'''
content = xmltodict.parse(t1, force_list=['servers', 'interfaces'])
data_str = json.dumps(content)
print(data_str)

output

{
  "servers": [
    {
      "server": {
        "name": "host1",
        "os": "Linux",
        "interfaces": [
          {
            "interface": {
              "name": "em0",
              "ip_address": "10.0.0.1"
            }
          }
        ]
      }
    }
  ]
}

is it possible to add similar parameters in java?

public JSONObject parse(String content, List<String> forceList) {
        // to be
        return  org.json.XML.toJSONObject(content);
    }

Activity

  1. stleary commented on Mar 27, 2021

    @stleary
    Owner

    Hi @relax-admin Sorry for the late reply. XML-to-JSON transformations are imperfect. We use XmlParserConfiguration to fine-tune the results. Feel free to post a pull request to implement the desired behavior.

  2. JanitSri commented on Oct 14, 2021

    @JanitSri
    Contributor

    I'm going try to tackle this issue @stleary

  3. stleary commented on Dec 6, 2021

    @stleary
    Owner

    I think this is fixed in #646

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions