Categories: JAXB

JAXB Unmarshalling Example

By the help of UnMarshaller interface, we can unmarshal(read) the object into xml document.In this example, we are going to convert simple xml document into java object.

Let’s see the steps to convert XML document into java object.

  1. Create POJO or bind the schema and generate the classes
  2. Create the JAXBContext object
  3. Create the Unmarshaller objects
  4. Call the unmarshal method
  5. Use getter methods of POJO to access the data

Unmarshaller example: Converting xml document into java object

Xml Document(employee.xml)

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee empid="1111">
    <address>
        <city>Noida</city>
        <locality>Sector-88</locality>
        <pin>201301</pin>
    </address>
    <address>
        <city>Kannauj</city>
        <locality>Kamala Nagar</locality>
        <pin>209724</pin>
    </address>
    <name>Dinesh Rajput</name>
    <salary>100000.0</salary>
</employee>

Employee.java

package com.doj.jaxb;

import java.util.List;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
 * @author Dinesh Rajput
 *
 */
@XmlRootElement
public class Employee {
 private int empid;  
    private String name;  
    private float salary;
    private List<Address> address;
 
    @XmlAttribute  
    public int getEmpid() {
  return empid;
 }
 public void setEmpid(int empid) {
  this.empid = empid;
 }
 @XmlElement  
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 @XmlElement  
 public float getSalary() {
  return salary;
 }
 public void setSalary(float salary) {
  this.salary = salary;
 }
 @XmlElement  
 public List<Address> getAddress() {
  return address;
 }
 public void setAddress(List<Address> address) {
  this.address = address;
 }
    
}

Address.java

package com.doj.jaxb;

/**
 * @author Dinesh Rajput
 *
 */
public class Address {
 private String locality;
 private String city;
 private int pin;
 
 public String getLocality() {
  return locality;
 }
 public void setLocality(String locality) {
  this.locality = locality;
 }
 public String getCity() {
  return city;
 }
 public void setCity(String city) {
  this.city = city;
 }
 public int getPin() {
  return pin;
 }
 public void setPin(int pin) {
  this.pin = pin;
 }
}

JaxbUnmarshalTest.java

package com.doj.jaxb;
import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;

/**
 * @author Dinesh Rajput
 *
 */
public class JaxbUnmarshalTest {

 /**
  * @param args
  * @throws JAXBException 
  */
 public static void main(String[] args) throws JAXBException {
  File file = new File("employee.xml");  
        JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);  
   
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
        Employee emp = (Employee) jaxbUnmarshaller.unmarshal(file);  
          
        System.out.println("Employee-"+emp.getEmpid()+" | "+emp.getName()+" | "+emp.getSalary());  
        System.out.println("Addresses: ");  
        List<Address> list = emp.getAddress();  
        for(Address address : list){  
         System.out.println(address.getLocality()+" | "+address.getCity()+" | "+address.getPin());
        }
 }
}

Run this application you get following output-
Employee-1111 | Dinesh Rajput | 100000.0
Addresses:
Sector-88 | Noida | 201301
Kamala Nagar | Kannauj | 209724

 

Download Source Code
JaxBUnmarshalExample.zip

 

<<Previous <<   || Index ||   >>Next >>

 

Previous
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago