Entities Class
Create new package named entities. In this package, create new java class named Student.java as below:
Student Entity
package entities;
import java.io.Serializable;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;
@XmlRootElement(name = "student")
public class Student implements Serializable {
private String id;
private String name;
private int age;
private double score;
private DoB doB;
private Phone phone;
public String getId() {
return id;
}
@XmlElement(name = "id")
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
@XmlElement(name = "name")
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
@XmlElement(name = "age")
public void setAge(int age) {
this.age = age;
}
public double getScore() {
return score;
}
@XmlElement(name = "score")
public void setScore(double score) {
this.score = score;
}
public DoB getDoB() {
return doB;
}
@XmlElement(name = "dob")
public void setDoB(DoB doB) {
this.doB = doB;
}
public Phone getPhone() {
return phone;
}
@XmlElement(name = "phone")
public void setPhone(Phone phone) {
this.phone = phone;
}
/* Declare phone tag */
@XmlRootElement(name = "phone")
public static class Phone implements Serializable {
private String value;
private String format;
public String getValue() {
return value;
}
@XmlValue
public void setValue(String value) {
this.value = value;
}
public String getFormat() {
return format;
}
@XmlAttribute(name = "format")
public void setFormat(String format) {
this.format = format;
}
public Phone() {
}
public Phone(String value, String format) {
this.value = value;
this.format = format;
}
}
/* Declare dob tag */
@XmlRootElement(name = "dob")
public static class DoB implements Serializable {
private String value;
private String format;
private String place;
public String getValue() {
return value;
}
@XmlValue
public void setValue(String value) {
this.value = value;
}
public String getFormat() {
return format;
}
@XmlAttribute(name = "format")
public void setFormat(String format) {
this.format = format;
}
public String getPlace() {
return place;
}
@XmlAttribute(name = "place")
public void setPlace(String place) {
this.place = place;
}
public DoB() {
}
public DoB(String value, String format, String place) {
this.value = value;
this.format = format;
this.place = place;
}
}
}
Run Application
Create new package, named demo. In this package, create new java class, named Main as below
package demo;
import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import entities.Student;
public class Main {
private static void serialize(Student student, String xmlFile) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Display result to console
marshaller.marshal(student, System.out);
// Save result to xml file
marshaller.marshal(student, new File(xmlFile));
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
private static void deSerialize(String xmlFile) {
try {
JAXBContext jaxbContext = JAXBContext.newInstance(Student.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Student student = (Student) unmarshaller.unmarshal(new File(xmlFile));
System.out.println("Student Info");
System.out.println("Id: " + student.getId());
System.out.println("Name: " + student.getName());
System.out.println("Age: " + student.getAge());
System.out.println("Score: " + student.getScore());
System.out.println("DoB: " + student.getDoB().getValue());
System.out.println("\tFormat: " + student.getDoB().getFormat());
System.out.println("\tPlace: " + student.getDoB().getPlace());
System.out.println("Phone: " + student.getPhone().getValue());
System.out.println("\tFormat: " + student.getPhone().getFormat());
} catch (Exception e) {
System.err.println(e.getMessage());
}
}
public static void main(String[] args) {
String xmlFile = "src\\data\\student.xml";
Student student = new Student();
student.setId("st01");
student.setName("name 1");
student.setAge(22);
student.setScore(7.8);
student.setDoB(new Student.DoB("1980-10-20", "yyyy-MM-dd", "New York"));
student.setPhone(new Student.Phone("123456", "US Phone"));
serialize(student, xmlFile);
deSerialize(xmlFile);
}
}
Output
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<student>
<age>22</age>
<dob format="yyyy-MM-dd" place="New York">1980-10-20</dob>
<id>st01</id>
<name>name 1</name>
<phone format="US Phone">123456</phone>
<score>7.8</score>
</student>
Student Info
Id: st01
Name: name 1
Age: 22
Score: 7.8
DoB: 1980-10-20
Format: yyyy-MM-dd
Place: New York
Phone: 123456
Format: US Phone