JAXB

Java Architecture for XML Binding – with POJO (Plain Old Java Object)


Converting POJO into XML (Marshalling) and Vice-versa (Unmarshalling).

Eclipse Oxygen. Create new project >> Java Project

Create stud.xml file.

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pojoEmp id="1163">
    <name>Melanie Dent</name>
    <role>Lara Croft</role>
</pojoEmp>

Create JAVA Files. Start with POJOEmp.java

package in.janisoftwares;

import javax.xml.bind.annotation.*;

@XmlRootElement
public class POJOEmp {
	
	private String name;
    private int id;
    private String role;

    /**
	 * 
	 */
	public POJOEmp() {

	}
	
	/**
	 * @param name
	 * @param id
	 * @param role
	 */
	public POJOEmp(String name, int id, String role) {
		this.name = name;
		this.id = id;
		this.role = role;
	}
	/**
	 * @return the name
	 */
	@XmlElement
	public String getName() {
		return name;
	}
	/**
	 * @param name the name to set
	 */
	public void setName(String name) {
		this.name = name;
	}
	/**
	 * @return the id
	 */
	@XmlAttribute
	public int getId() {
		return id;
	}
	/**
	 * @param id the id to set
	 */
	public void setId(int id) {
		this.id = id;
	}
	/**
	 * @return the role
	 */
	@XmlElement
	public String getRole() {
		return role;
	}
	/**
	 * @param role the role to set
	 */
	public void setRole(String role) {
		this.role = role;
	}
}

Create M.java (Basically Marshaller / Unmarshaller)

package in.janisoftwares;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class M {

	public static void main(String[] args) {
		try{
		    //Creating JAXB context
		    JAXBContext jContext = JAXBContext.newInstance(POJOEmp.class);
		    
		    //------UNMARSHALLING (READING FROM XML INTO POJO)-------//
		    Unmarshaller unMarshallObj = jContext.createUnmarshaller();
		    //Loading the object with XML data.
		    POJOEmp stud = (POJOEmp)unMarshallObj.unmarshal(new FileInputStream("stud.xml"));
		    //Displaying properties.
		    System.out.println("Name = " + stud.getName());
		    System.out.println("Id = " + stud.getId());
		    System.out.println("Role = " + stud.getRole());

		  //------MARSHALLING (WRITING A POJO INTO XML)-----------//
		    //Creating marshaller object
		    Marshaller marshallObj = jContext.createMarshaller();
		    //Setting property to show xml format output
		    marshallObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		    //Setting values in POJO class
		    POJOEmp emp2 = new POJOEmp("Anup", 1163, "Superman");
		    //Calling marshall method. Creates new file if doesn't exist.
		    marshallObj.marshal(emp2, new FileOutputStream("emp.xml"));
		} catch(Exception e) {
		    e.printStackTrace();
		}
	}
}

Run the M.java to get output as below

Name = Melanie Dent
Id = 1163
Role = Lara Croft

What just happened? It read stud.xml and displayed the results on console. Plus, it created emp.xml from POJOEmp type of Java object.

Congratulations! You successfully did your Marshalling (writing into XML) and Un-Marshalling (Reading from XML) using JAXB API.


Advertisement