Jump to: navigation, search

Difference between revisions of "EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Generating Schema"

(New page: '''Example:''' *http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JAXB/GenerateSchema)
 
m
Line 1: Line 1:
'''Example:'''
+
 
*http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JAXB/GenerateSchema
+
=Generating an XML Schema=
 +
 
 +
To generate an XML schema from a Java object model:
 +
 
 +
<ol>
 +
<li>Create a class that extends '''javax.xml.bind.SchemaOutputResolver'''.
 +
<source lang="java">
 +
private class MySchemaOutputResolver extends SchemaOutputResolver {
 +
 +
    public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
 +
        File file = new File(suggestedFileName);
 +
        StreamResult result = new StreamResult(file);
 +
        result.setSystemId(file.toURI().toURL().toString());
 +
        return result;
 +
    }
 +
 +
}
 +
</source></li>
 +
<li>Use an instance of this class with '''JAXBContext''' to capture the generated XML Schema.
 +
<source lang="java">
 +
Class[] classes = new Class[4];
 +
classes[0] = org.example.customer_example.AddressType.class;
 +
classes[1] = org.example.customer_example.ContactInfo.class;
 +
classes[2] = org.example.customer_example.CustomerType.class;
 +
classes[3] = org.example.customer_example.PhoneNumber.class;
 +
JAXBContext jaxbContext = JAXBContext.newInstance(classes);
 +
 +
SchemaOutputResolver sor = new MySchemaOutputResolver();
 +
jaxbContext.generateSchema(sor);
 +
</source></li>
 +
</ol>

Revision as of 13:59, 27 December 2010

Generating an XML Schema

To generate an XML schema from a Java object model:

  1. Create a class that extends javax.xml.bind.SchemaOutputResolver.
    private class MySchemaOutputResolver extends SchemaOutputResolver {
     
        public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
            File file = new File(suggestedFileName);
            StreamResult result = new StreamResult(file);
            result.setSystemId(file.toURI().toURL().toString());
            return result;
        }
     
    }
  2. Use an instance of this class with JAXBContext to capture the generated XML Schema.
    Class[] classes = new Class[4]; 
    classes[0] = org.example.customer_example.AddressType.class; 
    classes[1] = org.example.customer_example.ContactInfo.class; 
    classes[2] = org.example.customer_example.CustomerType.class; 
    classes[3] = org.example.customer_example.PhoneNumber.class; 
    JAXBContext jaxbContext = JAXBContext.newInstance(classes);
     
    SchemaOutputResolver sor = new MySchemaOutputResolver();
    jaxbContext.generateSchema(sor);