Saturday 20 August 2016

JAXB: Unmarshal data using generated ObjectFactory class

This is continuation to my previous post, You can get all the following model classes from previous post.

Address.java
Details.java
Employee.java
Employees.java
Project.java
ObjectFactory.java

Unmarshalling is the process of converting  XML document to Java object. Following example converts employee.xml document to Java object.


employee.xml
<?xml version="1.0" encoding="UTF-8"?>

<employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="employees.xsd">

 <employee>
  <id>1</id>
  <description> I am IT professional with 3Yrs of Experience </description>
  <taxExemption>240000</taxExemption>
  <taxPaid>89000.23</taxPaid>
  <descendants>3</descendants>
  <empNum>123456789</empNum>
  <specialities>C Java Threads XML Threads</specialities>
  <satifiedWithWork>true</satifiedWithWork>
  <details>
   <firstname>Hari</firstname>
   <middlename>Krishna</middlename>
   <lastname>Gurram</lastname>
   <age>25</age>
   <salary>80000</salary>
   <married>single</married>
   <sex>male</sex>
   <address>
    <city>Ongole</city>
    <district>prakasam</district>
    <state>Andhra Pradesh</state>
    <country>India</country>
    <pin>523169</pin>
   </address>
  </details>

  <project>
   <department>aero</department>
   <pjtname>Flight Controls</pjtname>
   <from>2011-09-16</from>
   <to>2013-01-22</to>
  </project>

  <project>
   <department>banking</department>
   <pjtname>Online Transaction Processing System</pjtname>
   <from>2013-01-23</from>
   <to>2014-10-06</to>
  </project>

  <project>
   <department>mobile</department>
   <pjtname>Daily News Application</pjtname>
   <from>2014-10-07</from>
   <to>2015-05-06</to>
  </project>
 </employee>
</employees>

Step 1: Get an instance of JAXBContext.

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

Step 2: Get Unmarshaller instance, using the jaxbContext object.

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

Step 3: Use ‘unmarshal’ method to convert given xml to object.

Employees emps = (Employees) unmarshaller.unmarshal(new File("/Users/harikrishna_gurram/Documents/employee.xml"));


Test.java
package generated;

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 Test {

 public static void main(String args[]) throws JAXBException {
  JAXBContext jaxbContext = JAXBContext.newInstance(ObjectFactory.class);

  Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

  Employees emps = (Employees) unmarshaller
    .unmarshal(new File("/Users/harikrishna_gurram/Documents/employee.xml"));

  List<Employee> empolyees = emps.getEmployee();

  for (Employee emp : empolyees) {
   System.out.println(emp.getDescendants());
   System.out.println(emp.getDescription());
  }

 }
}


Output
3
I am IT professional with 3Yrs of Experience 



Previous                                                 Next                                                 Home

No comments:

Post a Comment