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!
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- [Fix] bash: script.sh: /bin/bash^M: bad interpreter: No such file or directory - Bash
- Bootstrap tooltip not working - Bootstrap
- [fix] RabbitMQ: AuthenticationFailureException ACCESS_REFUSED Login refused authentication mechanism PLAIN - 2022
- Steps to Compare Two files in Sublime Text Side-by-Side - Sublime-Text
- Random Date Generator Tool - Tools
- Java 8 Predicate Functional Interface with Examples (Lambda) - Java
- Fahrenheit to Celsius Temperature Convertor: Tool & Formula - Tools
- ls: .: Operation not permitted - Mac Terminal ZSH Error - MacOS