Nov 5, 2013

JAXB Interview Questions and Answers: Marshalling

This blog posts extends JAXB Interview Questions and Answers: Unmarshalling.

Q. Can you create your Java objects from XSDs?
A. Yes, you can by binding a schema. Binding a schema means generating a set of Java classes that represents the schema. All JAXB implementations provide a tool called a binding compiler to bind a schema (the way the binding compiler is invoked can be implementation-specific). -p myapp13.model employee.xsd -d work

The -p option identifies a package for the generated classes, and the -d option identifies a target directory. So for this command, the classes are packaged in myapp13.model within the work directory. In response, the binding compiler generates a set of interfaces and a set of classes that implement the interfaces. The is generated conatining methods for generating instances of the interfaces.

Step 1: Define Java object that gets converted to XML string.

package com.mycompany.app12;

import javax.xml.bind.annotation.XmlRootElement;

public class Employee
    private Integer id;
    private String name;
    //JAXB requires a default constructor.
    private Employee(){}
    public Employee(Integer id, String name)
        super(); = id; = name;
    public Integer getId()
        return id;
    public void setId(Integer id)
    { = id;
    public String getName()
        return name;
    public void setName(String name)
    { = name;
    public String toString()
        return "Person [id=" + id + ", name=" + name + "]";

Step 2: Define the Marshaller utility class that works for all object types.

package com.mycompany.app13;


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

public class JAXBMarshaller
    public String marshalObject(Object object)
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            JAXBContext context = JAXBContext.newInstance(object.getClass());
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(object, outputStream);
        catch (JAXBException e)
            throw new RuntimeException("Error marshalling class: " + object.getClass() + "\n" + e.getMessage());
        String outputStreamToString = outputStream.toString();
        if (!outputStreamToString.isEmpty())
            return outputStreamToString;
        return null;

Step 3: Define the test class with a main method to run it stand alone.

package com.mycompany.app13;

public class Test
    public static void main(String[] args)
        //Construct the in memory object first
        Employee emp1 = new Employee(5, "Sam");
        JAXBMarshaller mashaller = new JAXBMarshaller();
        String marshalObject = mashaller.marshalObject(emp1);

Step 4: The ouput.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

You may also like:

Labels: ,


Post a Comment

Subscribe to Post Comments [Atom]

<< Home