- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 XML to POJO using the Jackson library in Java?
The JSON Jackson is a library for Java. It has very powerful data binding capabilities and provides a framework to serialize custom java objects to JSON and deserialize JSON back to Java object. We can also convert an XML format to the POJO object using the readValue() method of the XmlMapper class.
Syntax
public <T> T readValue(XMLStreamReader r, Class<T> valueType) throws IOException
Example
import com.fasterxml.jackson.dataformat.xml.*; public class XMLToPOJOTest { public static void main(String args[]) throws Exception { try { XmlMapper xmlMapper = new XmlMapper(); Person pojo = xmlMapper.readValue(getXmlString(), Person.class); System.out.println(pojo); } catch(Exception e) { e.printStackTrace(); } } private static String getXmlString() { return "<person> <firstName>Adithya</firstName>" + "<lastName>Jai</lastName>" + "<address>Bangalore</address>" + "</person>"; } } // Person class (POJO) 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; } public String toString() { return "Person[ " + "firstName = " + firstName + ", lastName = " + lastName + ", address = " + address + " ]"; } }
Output
Person[ firstName = Adithya, lastName = Jai, address = Bangalore ]
- Related Articles
- Convert POJO to XML 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?
- How to convert JsonNode to ArrayNode using Jackson API in Java?
- Convert Java object to JSON using the Gson library in Java?
- How to convert a JSON object to an enum using Jackson in Java?

Advertisements