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

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:

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

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

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

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

package euro2008;

import java.io.File;
import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;


public class XSDGenerator {
public XSDGenerator() {
}

public static void main(String[] args) throws JAXBException, IOException {

class MySchemaOutputResolver extends SchemaOutputResolver {
File baseDir = new File(".");

public Result createOutput(String namespaceUri,
String suggestedFileName) throws IOException {
return new StreamResult(new File(baseDir, suggestedFileName));
}
}

JAXBContext context = JAXBContext.newInstance(Country.class, Match.class);
context.generateSchema(new MySchemaOutputResolver());
}
}
 

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: 

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

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 .

3 Comments

  1. Lucas Jellema October 12, 2008
  2. Halil October 10, 2008
  3. nathalie roman June 24, 2008