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

POJO classes

File: Question.java
File: Answer.java

Unmarshaller class

File: XmlToObject.java

Output:

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