This post will explain how to generate schema files for java classes programmatically. The schema is generated by an instance of JAXBContext. In addition to JAXBContext we also need SchemaOutputResolver for this purpose. The SchemaOutputResolver class is an abstract class so we need to create a new class extending SchemaOutputResolver as shown below.
package JAXB;
import java.io.IOException;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
public class CustomSchemaOutputResolver extends SchemaOutputResolver {
@Override
public Result createOutput(String nameSpaceURI, String suggestedName) throws IOException {
StreamResult streamResult = new StreamResult(suggestedName);
return streamResult;
}
}
This class is used to create an object that represents the output to which the generated schema is sent. The output object is represented by Result Interface and StreamResult is one of the classes which implements the Result interface. StreamResult instance holds the schema generated by JAXBContext and writes it to the file with a name suggested by suggestedName variable. Below is the main java code
Main Class
package JAXB;
import java.io.IOException;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
public class JAXBMarshalDemo2 {
public static void main(String[] args) throws JAXBException, IOException {
JAXBContext jaxbContext = JAXBContext.newInstance(Country.class);
jaxbContext.generateSchema(new CustomSchemaOutputResolver());
}
}
Country class
package JAXB;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Country {
private ZipCode code;
private String name;
private int population;
public ZipCode getCode() {
return code;
}
public void setCode(ZipCode code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getPopulation() {
return population;
}
public void setPopulation(int population) {
this.population = population;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{").append("code:").append(this.code).append(",");
sb.append("name:").append(this.name).append(",");
sb.append("population:").append(this.population).append("}");
return sb.toString();
}
}
ZipCode
=============
package JAXB;
public class ZipCode {
private int part1;
private int part2;
public int getPart1() {
return part1;
}
public void setPart1(int part1) {
this.part1 = part1;
}
public int getPart2() {
return part2;
}
public void setPart2(int part2) {
this.part2 = part2;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("{").append("part1:").append(this.part1).append(",");
sb.append("part2:").append(this.part2).append("}");
return sb.toString();
}
}
Output (file name: schema1.xsd)
<?xml version=”1.0″ encoding=”UTF-8″ standalone=”yes”?>
<xs:schema version=”1.0″ xmlns:xs=”http://www.w3.org/2001/XMLSchema”>
<xs:element name=”country” type=”country”/>
<xs:complexType name=”country”>
<xs:sequence>
<xs:element name=”code” type=”zipCode” minOccurs=”0″/>
<xs:element name=”name” type=”xs:string” minOccurs=”0″/>
<xs:element name=”population” type=”xs:int”/>
</xs:sequence>
</xs:complexType>
<xs:complexType name=”zipCode”>
<xs:sequence>
<xs:element name=”part1″ type=”xs:int”/>
<xs:element name=”part2″ type=”xs:int”/>
</xs:sequence>
</xs:complexType>
</xs:schema>