utility to generate xsd from java class

You can use the generateSchema API on JAXBContext to generate an XML schema:

import java.io.IOException;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Node.class);   
        jc.generateSchema(new SchemaOutputResolver() {

            @Override
            public Result createOutput(String namespaceURI, String suggestedFileName)
                throws IOException {
                return new StreamResult(suggestedFileName);
            }

        });

    }

}

If you're already using JAXB, you can use the schemagen tool for creating an XSD:

  • http://docs.oracle.com/javase/6/docs/technotes/tools/share/schemagen.html
  • http://publib.boulder.ibm.com/infocenter/wasinfo/v7r0/index.jsp?topic=%2Fcom.ibm.websphere.express.doc%2Finfo%2Fexp%2Fae%2Ftwbs_jaxbjava2schema.html

There are also Ant tasks and Maven plugins for doing the same in an automated fashion.


If you are using Eclipse IDE, you can follow the steps from this official documentation:

Generating Schema from Classes

  1. From the Navigator or Project Explorer, select File > New > Other. The Select a wizard dialog appears.
  2. Select JAXB > Schema from JAXB Classes and then click Next. The Generate Schema from Classes page of the Generate Schema from JAXB Classes Wizard appears.
  3. Select the project, package, or classes from which to generate the schema and click Finish.

Tags:

Java

Xsd