Skip to main content

Notice: This Wiki is now read only and edits are no longer possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.

Jump to: navigation, search

EclipseLink/Examples/MOXy/BVinJAXB/SchemaGeneration

Generating schema with constraints

One of the features offered by BV in JAXB is the ability to dynamically generate XML Schema with XML Facets applied from the annotated java model.

Example:

package example.gettingstarted;
 
import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;
import java.io.File;
import java.io.IOException;
 
public class Main {
    public static void main(String[] args) throws Exception {
 
        JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
 
        SchemaOutputResolver sor = new MySchemaOutputResolver();
        jaxbContext.generateSchema(sor);
    }
 
    static class MySchemaOutputResolver extends SchemaOutputResolver {
 
        public Result createOutput(String uri, String suggestedFileName) throws IOException {
            File file = new File(suggestedFileName);
            StreamResult result = new StreamResult(file);
            result.setSystemId(file.toURI().toURL().toString());
            return result;
        }
    }
 
}

Back to the top