
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert POJO to XML using the Jackson library in Java?
A Jackson is a Java-based library and it can be useful to convert Java objects to JSON and JSON to Java Object. A Jackson API is faster than other API, needs less memory area and is good for the large objects. We convert a POJO to XML format using the writeValueAsString() method of XmlMapper class and we need to pass the corresponding POJO instance as an argument to this method.
Syntax
public String writeValueAsString(Object value) throws JsonProcessingException
Example
import com.fasterxml.jackson.dataformat.xml.*; public class POJOToXmlTest { public static void main(String args[]) throws Exception { try { XmlMapper xmlMapper = new XmlMapper(); Person pojo = new Person(); pojo.setFirstName("Raja"); pojo.setLastName("Ramesh"); pojo.setAddress("Hyderabad"); String xml = xmlMapper.writeValueAsString(pojo); System.out.println(xml); } catch(Exception e) { e.printStackTrace(); } } } // Person class class Person { private String firstName; private String lastName; private String address; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } }
Output
<Person xmlns=""> <firstName>Raja</firstName> <lastName>Ramesh</lastName> <address>Hyderabad</address> </Person>
- Related Questions & Answers
- Convert XML to POJO using the Jackson library in Java?
- Convert CSV to JSON using the Jackson library in Java?
- Convert JSON to/from Map using Jackson library in Java?
- How to convert Java object to JSON using Jackson library?
- How to convert a JSON to Java Object using the Jackson library in Java?
- How to convert a List to JSON array using the Jackson library in Java?
- Pretty print JSON using Jackson library in Java?
- How to implement a custom serializer using the Jackson library in Java?
- How to serialize the order of properties using the Jackson library in Java?
- How to ignore the null and empty fields using the Jackson library in Java?
- How can we format a date using the Jackson library in Java?
- How to ignore a field of JSON object using the Jackson library in Java?
- Convert Java object to JSON using the Gson library in Java?
- How to convert JsonNode to ArrayNode using Jackson API in Java?
- Convert a Map to JSON using the Gson library in Java?
Advertisements