Unmarshalling is the process of converting xml into java object. The unmarshal() method of JAXB Unmarshaller is used for unmarshalling process.
Steps:
1. Create a pojo class.
2. create JAXB context instance.
3. Create Unmarshaller instance using JAXB context.
4. Call unmarshal() method for unmarshalling process.
5. Process the pojo object.
Example explanation:
Below example have two pojo classes Student and Subject. A Student can have one or more Subject. Pass Student.xml file (generated from previous example) as a parameter in unmarshal() method and cast the return object to Student. Now we have Student object and can process it.
Example:
Student.java
import java.util.List; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlTransient; /** * This class represents a Student. * @author javawithease */ @XmlRootElement public class Student { private String name; private String rollNo; private String className; private int age; private int id; private List<Subject> subject; //Default constructor public Student(){ } //Parameterised constructor public Student(String name, String rollNo, String className, int age, int id, List<Subject> subject){ this.name = name; this.rollNo = rollNo; this.className = className; this.age = age; this.id = id; this.subject = subject; } public String getName() { return name; } @XmlElement public void setName(String name) { this.name = name; } public String getClassName() { return className; } @XmlElement public void setClassName(String className) { this.className = className; } public String getRollNo() { return rollNo; } @XmlElement public void setRollNo(String rollNo) { this.rollNo = rollNo; } public int getAge() { return age; } @XmlTransient public void setAge(int age) { this.age = age; } public int getId() { return id; } @XmlAttribute public void setId(int id) { this.id = id; } public List<Subject> getSubject() { return subject; } @XmlElement public void setSubject(List<Subject> subject) { this.subject = subject; } } |
Subject.java
/** * This class represents Address. * @author javawithease */ public class Subject { private String subjectname; private String subjectId; //Default constructor public Subject(){ } //Parameterised constructor public Subject(String subjectname, String subjectId){ this.subjectname = subjectname; this.subjectId = subjectId; } public String getSubjectname() { return subjectname; } public void setSubjectname(String subjectname) { this.subjectname = subjectname; } public String getSubjectId() { return subjectId; } public void setSubjectId(String subjectId) { this.subjectId = subjectId; } } |
JAXBTest.java
import java.io.File; import javax.xml.bind.JAXBContext; import javax.xml.bind.Unmarshaller; /** * This is test class for Unmarshalling. * @author javawithease */ public class JAXBTest { public static void main(String args[]){ try { //create JAXB context JAXBContext context = JAXBContext.newInstance(Student.class); //Create Unmarshaller using JAXB context Unmarshaller unmarshaller = context.createUnmarshaller(); Student student = (Student) unmarshaller.unmarshal(new File("D:\\Student.xml")); //Process the Student object System.out.println("Student name: " + student.getName()); System.out.println("Student rollNo: " + student.getRollNo()); for(Subject Subject: student.getSubject()){ System.out.println("Subject Name: " + Subject.getSubjectname()); System.out.println("Subject Id: " + Subject.getSubjectId()); } } catch (Exception e) { e.printStackTrace(); } } } |
Output:
Student name: jai Student rollNo: MCA/07/06 Subject Name: Java Subject Id: 1 Subject Name: Oracle Subject Id: 2 |
No comments:
Post a Comment