Convert JSON to XML in Spring Boot
In this example tutorial, we will show you how to convert JSON data to XML in Spring Boot.
Required Dependencies
Add the following dependencies to your Spring Boot project:
- Jackson Dataformat XML - This library helps to serialize POJOs to XML and deserialize XML to POJOs.
Add to the pom.xml file:
For Gradle
Add to the build.gradle file:
implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.13.1'
You can find the other versions of Jackson Dataformat XML in the Jackson Dataformat XML Maven repository.
The following code snippet converts JSON into XML:
ObjectMapper jsonMapper = new ObjectMapper();
try {
JsonNode jsonNode = jsonMapper.readValue(json, JsonNode.class);
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);
xmlMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
ObjectWriter ow = xmlMapper.writer().withRootName(rootName);
StringWriter sw = new StringWriter();
ow.writeValue(sw, jsonNode);
String xml = sw.toString();
} catch (StreamWriteException e) {
e.printStackTrace();
} catch (DatabindException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Here is the complete running code:
package com.xml.json.conversion.sample.converter;
import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import com.fasterxml.jackson.core.exc.StreamWriteException;
import com.fasterxml.jackson.databind.DatabindException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
public class JsonToXmlExample {
public String convertJsonToXml(String json, String rootName) {
ObjectMapper jsonMapper = new ObjectMapper();
try {
JsonNode jsonNode = jsonMapper.readValue(json, JsonNode.class);
XmlMapper xmlMapper = new XmlMapper();
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);
xmlMapper.configure(ToXmlGenerator.Feature.WRITE_XML_1_1, true);
xmlMapper.configure(SerializationFeature.INDENT_OUTPUT, true);
ObjectWriter ow = xmlMapper.writer().withRootName(rootName);
StringWriter sw = new StringWriter();
ow.writeValue(sw, jsonNode);
String xml = sw.toString();
return xml;
} catch (StreamWriteException e) {
e.printStackTrace();
} catch (DatabindException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public static void main(String[] args) throws IOException {
JsonToXmlExample example = new JsonToXmlExample();
String json = "{\"name\":\"Java Tutorials\",\"author\":\"Jake Parker\",\"language\":\"English\",\"price\":35.00}";
String xml = example.convertJsonToXml(json, "Product");
System.out.println(xml);
}
}
The above code gives the following output:
<?xml version='1.1' encoding='UTF-8'?>
<Product>
<name>Java Tutorials</name>
<author>Jake Parker</author>
<language>English</language>
<price>35.0</price>
</Product>
<Product>
<name>Java Tutorials</name>
<author>Jake Parker</author>
<language>English</language>
<price>35.0</price>
</Product>