JAXB XML String/File to Java Object Example

Below examples, shows how to convert XML file/String to Java Object (POJO) using JAXB

1) Java Data Object with JAXB Annotations

package com.kalliphant.jaxb.samples;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

// Controls whether fields or Javabean properties are serialized by default. 
// By default all public members will be searialized, 
// XmlAccessType.FIELD Serializes using FIELDS 
@XmlAccessorType(XmlAccessType.FIELD)
// Use @XmlRootElement to customize the XML root tag, by default it defaults
// to class name 'Employee'
@XmlRootElement(name = "employee")
public class Employee {

	private Long empId;

	// Below annotation is to customize the firstName XML tag
	@XmlElement(name = "first_name")
	private String firstName;

	private String lastName;

	private String designation;

	public Employee() {
	}

	public Employee(String firstName, String lastName, String designation) {
		super();
		this.firstName = firstName;
		this.lastName = lastName;
		this.designation = designation;
	}

	public Long getEmpId() {
		return empId;
	}

	public void setEmpId(Long empId) {
		this.empId = empId;
	}

	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 getDesignation() {
		return designation;
	}

	public void setDesignation(String designation) {
		this.designation = designation;
	}

	@Override
	public String toString() {
		return "Employee [empId=" + empId + ", firstName=" + firstName + ", lastName=" + lastName + ", designation="
				+ designation + "]";
	}
}

2) Convert XML String to Java Object/POJO Example

package com.kalliphant.jaxb.samples;

import java.io.StringReader;

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

public class XMLString2ObjectJaxb {

	public static void main(String[] args) {

		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

			StringReader reader = new StringReader("1234"
														+ "John"
														+ "Doe"
												  + "");
			Employee employee = (Employee) unmarshaller.unmarshal(reader);
			System.out.println(employee);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}

Console output

Employee [empId=1234, firstName=John, lastName=Doe, designation=null]

3) Convert XML File to Java Object/POJO Example

package com.kalliphant.jaxb.samples;

import java.io.File;

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

public class XMLFile2ObjectJaxb {

	public static void main(String[] args) {

		try {
			JAXBContext jaxbContext = JAXBContext.newInstance(Employee.class);
			Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

			Employee employee = (Employee) unmarshaller.unmarshal(new File("input.xml"));
			System.out.println(employee);
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

}

input.xml used

<employee>
	<empId>1234</empId>
	<first_name>John</first_name>
	<lastName>Doe</lastName>
	<designation>Manager</designation>
</employee>

Console output

Employee [empId=1234, firstName=John, lastName=Doe, designation=Manager]