Java and JAXB Tutorial – Marshaling

This blog posts extends Java and JAXB tutorial: 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).

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.

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

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

Step 4: The ouput.

Java Developer Interview Q&As

800+ Java Interview Q&As