Google

Jan 20, 2014

JAXB Interview Questions and Answers -- power of MOXy

This is an extension to JAXB Interview Questions and Answers - Unmarshalling.

Q. Why would be the motivating factor to use MOXy implementation of JAXB as opposed to the default implementation provided by the JDK6 implementation?

AUsing the @XmlPath annotation and other extensions provided by MOXy will make your implementation cleaner. For example. if we were to implement the previous example with some dependency class like Department, you need to define the Department POJO as follows to work with 

Default JDK implementation

package com.mycompany.app12;
 
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee
{
    private Integer id;
    private String name;
    
    @XmlElement(name="faculty")
    Department department;
     
    public Integer getId()
    {
        return id;
    }
     
    public void setId(Integer id)
    {
        this.id = id;
    }
     
    public String getName()
    {
        return name;
    }
     
    public void setName(String name)
    {
        this.name = name;
    }
    
    protected Department getDepartment() {
  return department;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", name=" + name + ", department=" + department + "]";
 } 
}


package com.mycompany.app12;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;


@XmlAccessorType(XmlAccessType.FIELD)
public class Department {
 
 private String name;

 protected String getName() {
  return name;
 }

 protected void setName(String name) {
  this.name = name;
 }

 @Override
 public String toString() {
  return "Department [name=" + name + "]";
 }
}


package com.mycompany.app12;
 
public class Test
{
     
    private static final String XML_STRING = "<employee>\r\n" +
            "    <id>123</id>\r\n" +
            "    <name>Peter</name>\r\n" +
            "     <faculty><name>finance</name></faculty> " +
            "</employee>";
     
    public static void main(String[] args)
    {
        JAXBUnMarshaller unmashaller = new JAXBUnMarshaller();
        Employee unmarshalObject = (Employee) unmashaller.unmarshalObject(Employee.class, XML_STRING);
        System.out.println(unmarshalObject);
    }
}


Output is:
jaxbContext is=jar:file:/C:/TOOLS/java/jdk1.7.0_40/jre/lib/rt.jar!/com/sun/xml/internal/bind/v2/runtime/JAXBContextImpl.class Build-Id: 1.7.0_40......
Employee [id=123, name=Peter, department=Department [name=finance]]


MOXy implementation

Step 1:  You need to bring in the MOXy implementation.

<dependency>
    <groupid>org.eclipse.persistence</groupId>
    <artifactid>eclipselink</artifactId>
    <version>2.3.2</version>
</dependency>

Step 2: Tell JAXB to use MOXy via jaxb.properties file and place it on where the POJOs like Employee is packaged.

javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory


Step 3: You will only need Employee.java and Department.java will no longer required. Thanks to the power of annotation @XmlPath("faculty/name/text( )"). Here is the revised Employee.java unmarshalling the same XML file with within employee.

package com.mycompany.app12;
 
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

import org.eclipse.persistence.oxm.annotations.XmlPath;
 
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee
{
     
    private Integer id;
    private String name;
    
    @XmlPath("faculty/name/text()")
    private String departmentName;
     
    public Integer getId()
    {
        return id;
    }
     
    public void setId(Integer id)
    {
        this.id = id;
    }
     
    public String getName()
    {
        return name;
    }
     
    public void setName(String name)
    {
        this.name = name;
    }
    
 protected String getDepartmentName() {
  return departmentName;
 }

 @Override
 public String toString() {
  return "Employee [id=" + id + ", name=" + name + ", departmentName=" + departmentName + "]";
 } 
}


MOXy Output:


jaxbContext is=org.eclipse.persistence.jaxb.JAXBContext@757869d9
Employee [id=123, name=Peter, departmentName=finance]


You may also like:

Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home