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).
|
1 2 |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
package com.mycompany.app13; 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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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.
|
1 2 3 4 5 6 7 |
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <employee> <id>5</id> <name>Sam</name> </employee> |