JAXB Marshalling Example: Converting Object into XML

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

JAXB Marshalling Example: Converting Object into XML

JAXB Marshalling Example: Converting Object into XML

By the help of Marshaller interface, we can marshal(write) the object into xml document. In the previous page, we have seen the simple example of converting object into xml.

In this example, we are going to convert the object into xml having primitives, strings and collection objects.

Let's see the steps to convert java object into XML document.

  • Create POJO or bind the schema and generate the classes
  • Create the JAXBContext object
  • Create the Marshaller objects
  • Create the content tree by using set methods
  • Call the marshal method
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 answers;  
  12. public Question() {}  
  13. public Question(int id, String questionname, List 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 getAnswers() {  
  35.     return answers;  
  36. }  
  37. public void setAnswers(List 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.   
File: ObjectToXml.java
  1. import java.io.FileOutputStream;  
  2. import java.util.ArrayList;  
  3.   
  4. import javax.xml.bind.JAXBContext;  
  5. import javax.xml.bind.Marshaller;  
  6.   
  7.   
  8. public class ObjectToXml {  
  9. public static void main(String[] args) throws Exception{  
  10.     JAXBContext contextObj = JAXBContext.newInstance(Question.class);  
  11.   
  12.     Marshaller marshallerObj = contextObj.createMarshaller();  
  13.     marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  
  14.   
  15.     Answer ans1=new Answer(101,"java is a programming language","ravi");  
  16.     Answer ans2=new Answer(102,"java is a platform","john");  
  17.       
  18.     ArrayList list=new ArrayList();  
  19.     list.add(ans1);  
  20.     list.add(ans2);  
  21.       
  22.     Question que=new Question(1,"What is java?",list);  
  23.     marshallerObj.marshal(que, new FileOutputStream("question.xml"));  
  24.        
  25. }  
  26.  

Output:

The generated xml file will look like this:

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 languageanswername>  
  5.         <id>101id>  
  6.         <postedby>ravipostedby>  
  7.     answers>  
  8.     <answers>  
  9.         <answername>java is a platformanswername>  
  10.         <id>102id>  
  11.         <postedby>johnpostedby>  
  12.     answers>  
  13.     <questionname>What is java?questionname>  
  14. question>