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 XML to JSON in Spring Boot
- Last updated Apr 25, 2024
Follow these steps to convert XML to JSON 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.
- Here's an example code to convert XML to JSON:
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'
First, create a POJO class representing the XML data structure:
public class MyClass {
private Long id;
private String name;
private double amount;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public double getAmount() {
return amount;
}
public void setAmount(double amount) {
this.amount = amount;
}
}
The code below demonstrates two ways of converting XML data to JSON: one by binding XML data to a POJO Java class and the other by directly converting the parsed XML data to JSON using JsonNode from com.fasterxml.jackson.databind package:
import java.io.IOException;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
public class Example {
public static String convertXmlToJson(String xml) {
XmlMapper xmlMapper = new XmlMapper();
try {
MyClass book = xmlMapper.readValue(xml, MyClass.class);
ObjectMapper objMapper = new ObjectMapper();
return objMapper.writeValueAsString(book);
} catch (JsonMappingException ex) {
ex.printStackTrace();
} catch (JsonProcessingException ex) {
ex.printStackTrace();
}
return null;
}
public static String convertXmlToJson2(String xml) {
XmlMapper xmlMapper = new XmlMapper();
JsonNode jsonNode;
try {
jsonNode = xmlMapper.readTree(xml.getBytes());
ObjectMapper objMapper = new ObjectMapper();
return objMapper.writeValueAsString(jsonNode);
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
public static void main(String[] args) {
String xml = "<MyClass><id>1</id><name>John</name><amount>10.5</amount></MyClass>";
String result1 = convertXmlToJson(xml);
System.out.println(result1);
String result2 = convertXmlToJson2(xml);
System.out.println(result2);
}
}
The output of the above code is as follows:
{"id":1,"name":"John","amount":10.5}
{"id":"1","name":"John","amount":"10.5"}