0

I am new to JAVA and I faced the following problem.

I have a JSON file and the target is to convert to XML. Conversion should be in the following format.

<a attribute1 = "" attribute2 = "" />

but the conversion happened as follows

<a><attribute1>value</attribute1><attribute2>value<attribute2></a>

How exactly can i convert maintaining the desired XML format?

Lucazade
  • 57
  • 5

3 Answers3

0
##Use json.jar for creating JSON to xml 
Steps:
1 .Add below mentioned artifacts to pom


<dependency>
        <groupId>org.json</groupId>
        <artifactId>json</artifactId>
        <version>20200518</version>
 </dependency>

2.Use below code template for converting JOSN to xml       
        JSONObject obj = new JSONObject(json_data);
        String xml_data = XML.toString(obj);
0

No standard library for JSON-to-XML or XML-to-JSON is ever going to give you exactly the format you want. Let the library create the XML it creates, and then transform it using an XSLT stylesheet. Alternatively do a custom conversion all the way from JSON to XML using XSLT 3.0.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
0

This json

{
   "a": {
      "-attribute1": "",
      "-attribute2": "",
      "-self-closing": "true"
   },
   "#omit-xml-declaration": "yes"
}

may be converted to this xml

<a attribute1="" attribute2=""/>

Underscore-java library

Valentyn Kolesnikov
  • 2,029
  • 1
  • 24
  • 31