JAXB Unmarshalling Example: Converting XML into Object

jaxb unmarshalling example, Convert XML to Object in java, jaxb example, converting object into xml example, java architecture for xml binding example, jaxb marshalling object into xml example

JAXB Unmarshalling Example: Converting XML into Object

JAXB Unmarshalling Example: Converting XML into Object

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.

  • Create POJO or bind the schema and generate the classes
  • Create the JAXBContext object
  • Create the Unmarshaller objects
  • Call the unmarshal method
  • Use getter methods of POJO to access the data

Unmarshaller example: Converting xml document into java object

Xml Document

File: question.xml
  1. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>  
  2. <question id="1">  
  3.     <answers>  
  4.         <answername>java is a programming language</answername>  
  5.         <id>101</id>  
  6.         <postedby>ravi</postedby>  
  7.     </answers>  
  8.     <answers>  
  9.         <answername>java is a platform</answername>  
  10.         <id>102</id>  
  11.         <postedby>john</postedby>  
  12.     </answers>  
  13.     <questionname>What is java?</questionname>  
  14. </question>  

POJO classes

File: Question.java
  1. import java.util.List;  
  2.   
  3. import javax.xml.bind.annotation.XmlAttribute;  
  4. import javax.xml.bind.annotation.XmlElement;  
  5. import javax.xml.bind.annotation.XmlRootElement;  
  6.   
  7. @XmlRootElement  
  8. public class Question {  
  9. private int id;  
  10. private String questionname;  
  11. private List<Answer> answers;  
  12. public Question() {}  
  13. public Question(int id, String questionname, List<Answer> answers) {  
  14.     super();  
  15.     this.id = id;  
  16.     this.questionname = questionname;  
  17.     this.answers = answers;  
  18. }  
  19. @XmlAttribute  
  20. public int getId() {  
  21.     return id;  
  22. }  
  23. public void setId(int id) {  
  24.     this.id = id;  
  25. }  
  26. @XmlElement  
  27. public String getQuestionname() {  
  28.     return questionname;  
  29. }  
  30. public void setQuestionname(String questionname) {  
  31.     this.questionname = questionname;  
  32. }  
  33. @XmlElement  
  34. public List<Answer> getAnswers() {  
  35.     return answers;  
  36. }  
  37. public void setAnswers(List<Answer> answers) {  
  38.     this.answers = answers;  
  39. }  
  40.  
File: Answer.java
  1. public class Answer {  
  2. private int id;  
  3. private String answername;  
  4. private String postedby;  
  5. public Answer() {}  
  6. public Answer(int id, String answername, String postedby) {  
  7.     super();  
  8.     this.id = id;  
  9.     this.answername = answername;  
  10.     this.postedby = postedby;  
  11. }  
  12. public int getId() {  
  13.     return id;  
  14. }  
  15. public void setId(int id) {  
  16.     this.id = id;  
  17. }  
  18. public String getAnswername() {  
  19.     return answername;  
  20. }  
  21. public void setAnswername(String answername) {  
  22.     this.answername = answername;  
  23. }  
  24. public String getPostedby() {  
  25.     return postedby;  
  26. }  
  27. public void setPostedby(String postedby) {  
  28.     this.postedby = postedby;  
  29. }  
  30.   

Unmarshaller class

File: XmlToObject.java
  1. import java.io.File;  
  2. import java.util.List;  
  3.   
  4. import javax.xml.bind.JAXBContext;  
  5. import javax.xml.bind.JAXBException;  
  6. import javax.xml.bind.Unmarshaller;  
  7.    
  8. public class XmlToObject {  
  9.     public static void main(String[] args) {  
  10.    
  11.      try {  
  12.    
  13.         File file = new File("question.xml");  
  14.         JAXBContext jaxbContext = JAXBContext.newInstance(Question.class);  
  15.    
  16.         Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  
  17.         Question que= (Question) jaxbUnmarshaller.unmarshal(file);  
  18.           
  19.         System.out.println(que.getId()+" "+que.getQuestionname());  
  20.         System.out.println("Answers:");  
  21.         List<Answer> list=que.getAnswers();  
  22.         for(Answer ans:list)  
  23.           System.out.println(ans.getId()+" "+ans.getAnswername()+"  "+ans.getPostedby());  
  24.    
  25.       } catch (JAXBException e) {  
  26.         e.printStackTrace();  
  27.       }  
  28.    
  29.     }  
  30. }  

Output:

1 What is java?
Answers:
101 java is a programming language ravi
102 java is a platform john