trainingtrains Logo

91-9990449935

 0120-4256464

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
File: Answer.java
File: ObjectToXml.java

Output:

The generated xml file will look like this:

File: question.xml