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?
A.
Step 1: You need eclipselink-xxx.jar in your classpath. So, include in pom.xml dependency.
<dependency>
<groupid>org.eclipse.persistence</groupId>
<artifactid>eclipselink</artifactId>
<version>2.5.1</version>
</dependency>
Step 2: You need jaxb.properties in your classpath with the following entry.
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Verify?
JAXBContext context = JAXBContext.newInstance(Employee.class);
System.out.println("JAXB Context is=" + context.toString());
Output:
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
<Employee> <name type="first">Peter</name> <age>25</age> </Employee>
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;
@XmlTransient
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@XmlElement
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
@XmlAttribute
@XmlPath("name/@type")
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;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("name=" + this.name);
if (type != null) {
sb.append("\ntype=" + type);
}
sb.append("\n");
sb.append("age=" + age);
return sb.toString();
}
}
Step 3: Marshalling and unmarshalling with MOXy.
package com.xml;
import java.io.ByteArrayInputStream;
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) {
e.printStackTrace();
}
}
}
Output:
JAXB Context is=org.eclipse.persistence.jaxb.JAXBContext@4501b7af Object: name=Peter type=first age=25 <?xml version="1.0" encoding="UTF-8"?> <Employee> <name type="first">Peter</name> <age>25</age> </Employee>
Labels: XML

0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home