Sai A Sai A
Updated date Jul 18, 2023
In this blog, we will explore different methods for converting JSON to XML using Java. The readers will learn how to utilize JSON libraries and XML libraries or specialized JSON-to-XML conversion libraries to achieve this transformation.

Introduction:

In today's world, the need to exchange data between different systems and applications is ever-present. However, data is often represented in various formats, leading to the common challenge of converting JSON (JavaScript Object Notation) to XML (eXtensible Markup Language). In this blog, we will explore practical and simplified methods to accomplish this conversion using Java. Let's dive into the world of Java JSON to XML conversion!

Method 1: Using a JSON Library and an XML Library

The first method involves utilizing a JSON library, such as Jackson or Gson, to parse the JSON input into a Java object. Subsequently, an XML library like JAXB or XStream can be employed to marshal the Java object into XML format. Let's look at a code example to understand the process:

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.StringWriter;

public class JSONtoXMLConverter {
    public static void main(String[] args) {
        String jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";

        try {
            // Step 1: Parse JSON using Jackson library
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(jsonString);

            // Step 2: Convert Java object to XML using JAXB library
            JAXBContext jaxbContext = JAXBContext.newInstance(JsonNode.class);
            Marshaller marshaller = jaxbContext.createMarshaller();
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

            StringWriter stringWriter = new StringWriter();
            marshaller.marshal(jsonNode, stringWriter);

            String xmlString = stringWriter.toString();
            System.out.println(xmlString);
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

Output:

<JsonNode>
  <name>John</name>
  <age>30</age>
  <city>New York</city>
</JsonNode>

Method 2: Using a JSON-to-XML Conversion Library

An alternative approach involves using a specialized library that directly converts JSON to XML, eliminating the need for an intermediate Java object representation. One such library is "json-to-xml" by org.json. Let's see how it can be implemented:

import org.json.JSONObject;
import org.json.XML;

public class JSONtoXMLConverter {
    public static void main(String[] args) {
        String jsonString = "{\"name\":\"John\", \"age\":30, \"city\":\"New York\"}";

        // Step 1: Convert JSON to XML using json-to-xml library
        JSONObject jsonObject = new JSONObject(jsonString);
        String xmlString = XML.toString(jsonObject);

        // Optional: Beautify XML output
        String beautifiedXmlString = XML.toString(jsonObject, "root");

        System.out.println(xmlString);
        System.out.println(beautifiedXmlString);
    }
}

Output:

<root><age>30</age><city>New York</city><name>John</name></root>
<root>
  <age>30</age>
  <city>New York</city>
  <name>John</name>
</root>

Conclusion:

In this blog, we explored two practical methods for converting JSON to XML using Java. The first method involved utilizing JSON and XML libraries, while the second method demonstrated the use of a specialized JSON-to-XML conversion library. 

Comments (0)

There are no comments. Be the first to comment!!!