Spring Boot
Spring Boot
Creating a Spring Boot Project
Making HTTP GET Requests with Query Parameters in Spring Boot
Making HTTP POST Requests in Spring Boot
Calling a REST API in Spring Boot
Validating Request Input in Spring Boot
Uploading Files in Spring Boot
Downloading Files in Spring Boot
Upload Image File as Base64 String in Spring Boot
How to Zip Multiple Files for Download in Spring Boot/Java
Spring Boot with MySQL
Spring Boot with MongoDB
Spring Data JPA Query Methods
Pagination and Sorting in Spring Boot
Enable Global CORS in Spring Boot
How to Write Unit Tests in Spring Boot - Best Practices and Examples
Convert XML to JSON in Spring Boot
Convert JSON to XML in Spring Boot
Change Logback Log Level Programmatically at Runtime via a REST API in Spring Boot
Exporting Data from MySQL Database to Excel in Spring Boot via REST API using Apache POI Library
Importing Excel Data into MySQL Database in Spring Boot via REST API using Apache POI Library
JWT Authentication using Spring Security OAuth2 in Spring Boot Example
Spring JPA Query to Check if the Current Date is Between a Range of Two Dates
Spring JPA Query to Find the Offered Price on the Current Date by Searching Between a Range of Two Dates
MongoDB Query to Find Ranking of Records in Spring Boot Java
Using Multiple Databases with Spring Data JPA in Spring Boot
Convert HTML to PDF for download in Spring Boot
Building Microservices using Spring Boot in 2024
Calling a Rest API using WebClient in Spring Boot
Convert JSON to XML in Spring Boot
- Last updated Apr 25, 2024
Follow these steps to convert JSON to XML in a Spring Boot application:
- Ensure that you have the necessary Jackson dependencies in your project. Add Jackson Dataformat XML library to your project. Jackson Dataformat XML helps to serialize POJOs to XML and deserialize XML to POJOs.
- The code below demonstrates how to convert data from JSON to XML in Spring Boot:
For Maven:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.15.2</version>
</dependency>
For Gradle:
implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.15.2'
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 JsonXMLExample {
public static 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 {
String json = "{\"name\":\"Java Tutorials\",\"author\":\"Jake Parker\",\"language\":\"English\",\"price\":35.00}";
String xml = convertJsonToXml(json, "Product");
System.out.println(xml);
}
}
The output of the above code is as follows:
<?xml version='1.1' encoding='UTF-8'?>
<Product>
<name>Java Tutorials</name>
<author>Jake Parker</author>
<language>English</language>
<price>35.0</price>
</Product>