Jul 28, 2014

Java and JAXB with MOXy implementation tutorial to convert XML to object and vice versa

Q. Why favor MOXy JAXB implementation?
A. JAXB is a specification for which there are many implementations like JDK default, MOXy, Metro, and JaxMeAPI. MOXy is from EclipseLink  enhances mapping with

1. @XmlPath extension which is inspired from XPath
2. The Jersey JAX-RS reference implementation provides JSON binding via MOXy.
3. MOXy is already the default JAXB implementation in WebLogic 12.1.1

Q. How to enable MOXy?

Step 1: You need eclipselink-xxx.jar in your classpath. So, include in pom.xml dependency.


Step 2: You need in your classpath with the following entry.



JAXBContext context = JAXBContext.newInstance(Employee.class);
System.out.println("JAXB Context is=" + context.toString());


JAXB Context is=org.eclipse.persistence.jaxb.JAXBContext@4501b7af

Now, remap Employee covered in tutorial without the nested Name object. The @XmlPath annotation from the MOXy to the rescue.

Step 1: The employee XML file

   <name type="first">Peter</name>

Step 2: The Employee  object with annotations to map to the above XML. Note the @XmlPath annotation with XPath syntax.

package com.xml;

import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

import org.eclipse.persistence.oxm.annotations.XmlPath;

@XmlRootElement(name = "Employee")
@XmlType(propOrder = { "name", "type", "age" })
public class Employee {

 private int id;
 private String name;
 private String type;
 private int age;

 public int getId() {
  return id;

 public void setId(int id) { = id;

 public String getName() {

 public void setName(String name) { = name;

 public String getType() {
  return type;

 public void setType(String type) {
  this.type = type;

 public int getAge() {
  return age;

 public void setAge(int age) {
  this.age = age;

 public String toString() {
  StringBuilder sb = new StringBuilder();
  sb.append("name=" +;
  if (type != null) {
   sb.append("\ntype=" + type);
  sb.append("age=" + age);
  return sb.toString();

Step 3: Marshalling and unmarshalling with MOXy.

package com.xml;


import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

public class JaxbMarshallUnMarshallWithMoxy {

 public static void main(String[] args) {

  String xml = "<Employee><name type=\"first\">Peter</name><age>25</age></Employee>";

  try {

   JAXBContext context = JAXBContext.newInstance(Employee.class);
   //Ensuring that we are using MOXy JAXB implementation instead of JDK default
   System.out.println("JAXB Context is=" + context.toString());

   // unmarshalling - XML to Java object
   Unmarshaller un = context.createUnmarshaller();
   Employee emp = (Employee) un.unmarshal(new ByteArrayInputStream(xml.getBytes()));

   System.out.println("Object: " + emp);

   // Marshalling - Java object to XML
   Marshaller m = context.createMarshaller();
   // for pretty-print XML in JAXB
   m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
   m.marshal(emp, System.out);

  } catch (JAXBException e) {



JAXB Context is=org.eclipse.persistence.jaxb.JAXBContext@4501b7af
Object: name=Peter
<?xml version="1.0" encoding="UTF-8"?>
   <name type="first">Peter</name>



Post a Comment

Subscribe to Post Comments [Atom]

Links to this post:

Create a Link

<< Home