Skip to content

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

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);
    }

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