How to quickly generate an XSD (XML Schema Definition) based on a Java Class Model using JDeveloper 11g

3

In the course of my current studies into XSD (XML Schema Definition) and how to convert from XSD to Java Class and vice versa as well as to Database Design vv. I ran into a very simple way of creating an XSD document based on Java Class definitions. With a very simple piece of code, you can generate the XSD document in no time at all, using JDeveloper 11g and its JAXB libraries. Note: the code presented in this article will work in any environment that has the JAXB libraries available.....

In my example, I have a small class model:

I ensure that two libraries are associated with the JDeveloper project: JAXB and JEE 1.5:

 

Now the following class can be used to generate the XSD document derived from this class model:

package euro2008;<br /><br />import java.io.File;<br />import java.io.IOException;<br /><br />import javax.xml.bind.JAXBContext;<br />import javax.xml.bind.JAXBException;<br />import javax.xml.bind.SchemaOutputResolver;<br />import javax.xml.transform.Result;<br />import javax.xml.transform.stream.StreamResult;<br /><br /><br />public class XSDGenerator {<br />    public XSDGenerator() {<br />    }<br /><br />    public static void main(String[] args) throws JAXBException, IOException {<br /><br />        class MySchemaOutputResolver extends SchemaOutputResolver {<br />            File baseDir = new File(&quot;.&quot;);<br /><br />            public Result createOutput(String namespaceUri, <br />                                       String suggestedFileName) throws IOException {<br />                return new StreamResult(new File(baseDir, suggestedFileName));<br />            }<br />        }<br /><br />        JAXBContext context = JAXBContext.newInstance(Country.class, Match.class);<br />        context.generateSchema(new MySchemaOutputResolver());<br />    }<br />}<br />&nbsp;

When I run this class, a file called schema1.xsd is generated into the root directory of the project. When I refresh the project in JDeveloper, it will show up as a new resource: 

 

Resources

I just noted that Edwin Biemond wrote a very useful blog article on JAXB in JDeveloper 10g and 11g. So please look here: http://biemond.blogspot.com/2008/02/jaxb-20-in-jdeveloper-10g-and-11g.html .

Share.

About Author

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director for Fusion Middleware. Consultant, trainer and instructor on diverse areas including Oracle Database (SQL & PLSQL), Service Oriented Architecture, BPM, ADF, Java in various shapes and forms and many other things. Author of the Oracle Press book: Oracle SOA Suite 11g Handbook. Frequent presenter on conferences such as JavaOne, Oracle OpenWorld, ODTUG Kaleidoscope, Devoxx and OBUG. Presenter for Oracle University Celebrity specials.

3 Comments

  1. Hi Halil, No you do not. I merely wanted to show how you can do this when using JDeveloper 11g. Not suggest that the only way to generate an XSD is using JDeveloper 11g. best regards, Lucas

  2. Hey Lucas,

    This is great stuff, I’m still looking for a declarative way to perform this step, but an automated way as you’ve blogged is awsome already!

    Thanks,
    Nathalie