In order to convert a JSON String into XML we will need to make use of the two Jackson Libraries,
Gradle:implementation group: 'com.fasterxml.jackson.core', name: 'jackson-databind', version: '2.13.4'
implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.13.4'
Maven:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.13.4</version>
</dependency>
We will first convert the JSON String to Java Object using ObjectMapper, then to XML String using XmlMapper class,
Employee POJO:public class Employee {
String empName;
int empAge;
String empDept;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public int getEmpAge() {
return empAge;
}
public void setEmpAge(int empAge) {
this.empAge = empAge;
}
public String getEmpDept() {
return empDept;
}
public void setEmpDept(String empDept) {
this.empDept = empDept;
}
}
JavaJsonToXML.java
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;
public class JavaJsonToXML {
public static void main(String[] args) {
try {
//Step 1: JSON String
String jsonString = "{\"empName\":\"Andy\",\"empAge\":22,\"empDept\":\"Insurance\"}";
ObjectMapper objectMapper = new ObjectMapper();
//Step 2: JSON String to Java Object
Employee employee = objectMapper.readValue(jsonString, Employee.class);
XmlMapper xmlMapper = new XmlMapper();
//3. JavaObject to XML String
String xmlString = xmlMapper.writeValueAsString(employee);
System.out.println(xmlString);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
<Employee><empName>Andy</empName><empAge>22</empAge><empDept>Insurance</empDept></Employee>
Facing issues? Have Questions? Post them here! I am happy to answer!
Author Info:
Rakesh (He/Him) has over 14+ years of experience in Web and Application development. He is the author of insightful How-To articles for Code2care.
Follow him on: X
You can also reach out to him via e-mail: rakesh@code2care.org
More Posts related to Java,
- Get the current timestamp in Java
- Java Stream with Multiple Filters Example
- Java SE JDBC with Prepared Statement Parameterized Select Example
- Fix: UnsupportedClassVersionError: Unsupported major.minor version 63.0
- [Fix] Java Exception with Lambda - Cannot invoke because object is null
- 7 deadly java.lang.OutOfMemoryError in Java Programming
- How to Calculate the SHA Hash Value of a File in Java
- Java JDBC Connection with Database using SSL (https) URL
- How to Add/Subtract Days to the Current Date in Java
- Create Nested Directories using Java Code
- Spring Boot: JDBCTemplate BatchUpdate Update Query Example
- What is CA FE BA BE 00 00 00 3D in Java Class Bytecode
- Save Java Object as JSON file using Jackson Library
- Adding Custom ASCII Text Banner in Spring Boot Application
- [Fix] Java: Type argument cannot be of primitive type generics
- List of New Features in Java 11 (JEPs)
- Java: How to Add two Maps with example
- Java JDBC Transition Management using PreparedStatement Examples
- Understanding and Handling NullPointerException in Java: Tips and Tricks for Effective Debugging
- Steps of working with Stored Procedures using JDBCTemplate Spring Boot
- Java 8 java.util.Function and BiFunction Examples
- The Motivation Behind Generics in Java Programming
- Get Current Local Date and Time using Java 8 DateTime API
- Java: Convert Char to ASCII
- Deep Dive: Why avoid java.util.Date and Calendar Classes
More Posts:
- Outlook and Hotmail not working - email issue, message not delivered, send receive problem - Microsoft
- How to Screenshot on Mac Ventura 13 - Step by Step Guide - MacOS
- How to Gracefully Close Jupyter Notebook - Python
- [Fix] Microsoft Store Error Code: 0x803F8001 - Microsoft
- How to Go To /usr/local/bin on Mac Terminal? - MacOS
- Android Emulator Screenshot saved location - Android-Studio
- How to download and install macOS 13 Ventura - MacOS
- Add Line Number before each line in Notepad++ using Column Editor - NotepadPlusPlus