Notice: this Wiki will be going read only early in 2024 and edits will no longer be possible. Please see: https://gitlab.eclipse.org/eclipsefdn/helpdesk/-/wikis/Wiki-shutdown-plan for the plan.
Difference between revisions of "EclipseLink/Examples/MOXy/JAXB/GenerateSchema"
< EclipseLink | Examples | MOXy | JAXB
(New page: == Overview == In this example you will learn how to generate an XML schema from a Java model. == Using JAXBContext to Generate an XML Schema == First you must create a class that exten...) |
m (added xref to user guide) |
||
Line 1: | Line 1: | ||
+ | {{EclipseLink_DocWiki | ||
+ | |link=EclipseLink/UserGuide/MOXy/Runtime/Bootstrapping/Generating_Schema}} | ||
+ | |||
+ | |||
== Overview == | == Overview == | ||
Latest revision as of 10:15, 4 January 2011
Overview
In this example you will learn how to generate an XML schema from a Java model.
Using JAXBContext to Generate an XML Schema
First you must 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; } }
Then 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);