Suggested Pages

Tuesday, January 31, 2017

convert xml to json


maven dependency

  <dependencies>
  <dependency>
   <groupId>org.json</groupId>
   <artifactId>json</artifactId>
   <version>20160810</version>
  </dependency>
   </dependencies>

Example of Java Code to convert XML into json

 int PRETTY_PRINT_INDENT_FACTOR = 4;
  JSONObject jsonObject = XML.toJSONObject(TEST_XML_STRING);
  String output = jsonObject.toString(PRETTY_PRINT_INDENT_FACTOR);
  System.out.println(output);


XML to json

<?xml version="1.0" ?>
<parent parentAttribute1="parentValue">
<child>2</child>
<child attribute="value">3
</child>Example
</parent>

output of json


{"parent": {
    "parentAttribute1": "parentValue",
    "content": "Example",
    "child": [
        2,
        {
            "attribute": "value",
            "content": 3
        }
    ]
}}

No comments :

Post a Comment

Suggested Pages