Google

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).

xjc.sh -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 ObjectFactory.java 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;

@XmlRootElement
public class Employee
{
    
    private Integer id;
    private String name;
    
    //JAXB requires a default constructor.
    private Employee(){}
    
    public Employee(Integer id, String name)
    {
        super();
        this.id = id;
        this.name = name;
    }
    
    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;
    }
    
    @Override
    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 java.io.ByteArrayOutputStream;

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();
        try
        {
            JAXBContext context = JAXBContext.newInstance(object.getClass());
            Marshaller m = context.createMarshaller();
            m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
            m.marshal(object, outputStream);
        }
        catch (JAXBException e)
        {
            e.printStackTrace();
            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);
        System.out.println(marshalObject);
    }
}

Step 4: The ouput.
 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <id>5</id>
    <name>Sam</name>
</employee>



You may also like:



Labels: ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home