
To convert a Java Object into XML we can make use of the Jackson Library, make sure to add the dependency in your project first,
Gradle:implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-xml', version: '2.13.4'
Maven:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-xml</artifactId>
<version>2.13.4</version>
</dependency>
Convert Java Object to XML (Pojo to XML)
Employee.javapublic class Employee {
String empName;
int empAge;
String empDept;
public Employee(String empName, int empAge, String empDept) {
this.empName = empName;
this.empAge = empAge;
this.empDept = 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;
}
}
JavaObjectToXML.java
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import java.io.IOException;
public class JavaObjectToXML {
public static void main(String[] args) {
try {
Employee emp = new Employee("Andy",45,"Insurance");
XmlMapper xmlMapper = new XmlMapper();
String xmlString = xmlMapper.writeValueAsString(emp);
System.out.println(xmlString);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
}
Output:
<Employee><empName>Andy</empName><empAge>45</empAge><empDept>Insurance</empDept></Employee>
-
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Java equals method - Tutorial
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- Spring Boot: @RequestBody not applicable to method
- Java 8: Steam map with Code Examples
- Java Program: Random Number Generator
- Java java.time.Clock class code examples [Java Date Time API]
- Fix: type argument is not within bounds of type-variable T
- [Fix] java.net.MalformedURLException: unknown protocol
- Java 7 addSuppression() and getSuppression() Exception Handling
- Convert Java Array to ArrayList Code Example
- How to Word-Warp Console logs in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Remove Trailing zeros BigDecimal Java
- CRUD operations in Spring Boot + JDBC
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- Json Serialization and Deserialization using Java Jackson
- Create simple struts2 project using maven commands
- How to install Java OpenJDK 11 on Alpine Linux
- Unsupported major.minor version 52.0 in java
- Error: Can not find the tag library descriptor for
- Java: Convert String to Binary
- How to run Java Unit Test cases with Apache Maven?
- Java: Testing Private Methods in JUnit using reflection API Example
- Java JDBC Connection with MySQL Driver in VS Code + Troubleshooting
- Java Join Strings with Comma Separator
More Posts:
- Git Fix: fatal: refusing to merge unrelated histories Error - Git
- [Fix] Instant java.time.temporal.UnsupportedTemporalTypeException: Unsupported unit: Years - Java
- Installing and using unzip Command to unzip a zip file using Terminal - Linux
- [Fix] Cannot connect to Microsoft SQL Server, Error: 18456 - Microsoft
- Check if a Java Date String is Valid or Not (Java 8) - Java
- Java Split String by Spaces - Java
- Tutorial Java SOAP WebServices JAS-WS with Eclipse J2EE IDE and Tomcat Server Part 1 - Java
- Fix: ZSH: cd: too many arguments (macOS) - zsh