Login
Register

Home

Trainings

Fusion Blog

EBS Blog

Authors

CONTACT US

Fusion Blog
  • Register

Oracle Gold Partners, our very popular training packages, training schedule is listed here
Designed by Five Star Rated Oracle Press Authors & Oracle ACE's.

webinar new

Search Courses

Objective:

In the previous article Parsing an XML file using SAX we learned about parsing an XML file using SAX. In the article we will learn about How to convert an object into XML document and XML document into Object.


Converting Object to XML in Java:

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.

 

  1. Create POJO or bind the schema and generate the classes

  2. Create the JAXBContext object

  3. Create the Marshaller objects

  4. Create the content tree by using set methods

  5. Call the marshal method

Example 1:

File: Question.java

import java.util.List;  

import javax.xml.bind.annotation.XmlAttribute;  

import javax.xml.bind.annotation.XmlElement;  

import javax.xml.bind.annotation.XmlRootElement;  

@XmlRootElement  

public class Question {  

private int id;  

private String questionname;  

private List<Answer> answers;  

public Question() {}  

public Question(int id, String questionname, List<Answer> answers) {  

   super();  

   this.id = id;  

   this.questionname = questionname;  

   this.answers = answers;  

}  

@XmlAttribute  

public int getId() {  

   return id;  

}  

public void setId(int id) {  

   this.id = id;  

}  

@XmlElement  

public String getQuestionname() {  

   return questionname;  

}  

public void setQuestionname(String questionname) {  

   this.questionname = questionname;  

}  

@XmlElement  

public List<Answer> getAnswers() {  

   return answers;  

}  

public void setAnswers(List<Answer> answers) {  

   this.answers = answers;  

}  

}


File: Answer.java

public class Answer {  

private int id;  

private String answername;  

private String postedby;  

public Answer() {}  

public Answer(int id, String answername, String postedby) {  

   super();  

   this.id = id;  

   this.answername = answername;  

   this.postedby = postedby;  

}  

public int getId() {  

   return id;  

}  

public void setId(int id) {  

   this.id = id;  

}  

public String getAnswername() {  

   return answername;  

}  

public void setAnswername(String answername) {  

   this.answername = answername;  

}  

public String getPostedby() {  

   return postedby;  

}  

public void setPostedby(String postedby) {  

this.postedby = postedby;  

}  

}  

File: ObjectToXml.java

import java.io.FileOutputStream;  

import java.util.ArrayList;  

import javax.xml.bind.JAXBContext;  

import javax.xml.bind.Marshaller;  

public class ObjectToXml {  

public static void main(String[] args) throws Exception{  

   JAXBContext contextObj = JAXBContext.newInstance(Question.class);  

   Marshaller marshallerObj = contextObj.createMarshaller();  

   marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);  

   Answer ans1=new Answer(101,"java is a programming language","ravi");  

   Answer ans2=new Answer(102,"java is a platform","john");  

   ArrayList<Answer> list=new ArrayList<Answer>();  

   list.add(ans1);  

   list.add(ans2);

Question que=new Question(1,"What is java?",list);  

marshallerObj.marshal(que, new FileOutputStream("question.xml"));  

}  

}  

//Output:

The generated XML file :

File: Question.xml  

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>  

<question id="1">

<answers>  

<answername>java is a programming language</answername>

<id>101</id>

<postedby>ravi</postedby>  

</answers>  

<answers>  

<answername>java is a platform</answername>  

<id>102</id>  

postedby>john</postedby>  

</answers>  

<questionname>What is java?</questionname>  

</question>  

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.

  1. Create POJO or bind the schema and generate the classes

  2. Create the JAXBContext object

  3. Create the Unmarshaller objects

  4. Call the unmarshal method

  5. Use getter methods of POJO to access the data

XML Document

File: question.xml

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>  

<question id="1">  

<answers>  

<answername>java is a programming language</answername>  

<id>101</id>  

<postedby>ravi</postedby>  

</answers>  

<answers>  

<answername>java is a platform</answername>  

<id>102</id>  

<postedby>john</postedby>  

</answers>  

<questionname>What is java?</questionname>  

</question>


POJO classes

File: Question.java

import java.util.List;  

import javax.xml.bind.annotation.XmlAttribute;  

import javax.xml.bind.annotation.XmlElement;  

import javax.xml.bind.annotation.XmlRootElement;  

@XmlRootElement  

public class Question {  

private int id;  

private String questionname;  

private List<Answer> answers;  

public Question() {}  

public Question(int id, String questionname, List<Answer> answers) {  

   super();  

   this.id = id;  

   this.questionname = questionname;  

   this.answers = answers;  

}  

@XmlAttribute  

public int getId() {  

   return id;  

}  

public void setId(int id) {  

   this.id = id;  

}  

@XmlElement  

public String getQuestionname() {  

   return questionname;  

}  

public void setQuestionname(String questionname) {  

   this.questionname = questionname;  

}  

@XmlElement  

public List<Answer> getAnswers() {  

   return answers;  

}  

public void setAnswers(List<Answer> answers) {  

   this.answers = answers;  

}  

}  

File: Answer.java

public class Answer {  

private int id;  

private String answername;  

private String postedby;  

public Answer() {}  

public Answer(int id, String answername, String postedby) {  

super();   

this.id = id;  

this.answername = answername;  

this.postedby = postedby;  

}  

public int getId() {  

   return id;  

}  

public void setId(int id) {  

   this.id = id;  

}  

public String getAnswername() {  

   return answername;  

}  

public void setAnswername(String answername) {  

   this.answername = answername;  

}  

public String getPostedby() {  

   return postedby;  

}  

public void setPostedby(String postedby) {  

   this.postedby = postedby;  

}  

}  

Unmarshaller class

File: XmlToObject.java

import java.io.File;  

import java.util.List;  

import javax.xml.bind.JAXBContext;  

import javax.xml.bind.JAXBException;  

import javax.xml.bind.Unmarshaller;  

public class XmlToObject {  

public static void main(String[] args) {  

try {  

       File file = new File("question.xml");  

       JAXBContext jaxbContext = JAXBContext.newInstance(Question.class);  

       Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  

       Question que= (Question) jaxbUnmarshaller.unmarshal(file);  

       System.out.println(que.getId()+" "+que.getQuestionname());  

       System.out.println("Answers:");  

       List<Answer> list=que.getAnswers();  

       for(Answer ans:list)  

         System.out.println(ans.getId()+" "+ans.getAnswername()+"  "+ans.getPostedby());  

     } catch (JAXBException e) {  

       e.printStackTrace();  

     }  

  }  

}  

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









Varun Kapila

Add comment


Security code
Refresh

About the Author

Varun Kapila

Search Trainings

Fully verifiable testimonials

Apps2Fusion - Event List

<<  May 2024  >>
 Mon  Tue  Wed  Thu  Fri  Sat  Sun 
    1  2  3  4  5
  6  7  8  9101112
13141516171819
20212223242526
2728293031  

Enquire For Training

Fusion Training Packages

Get Email Updates


Powered by Google FeedBurner