Generate an XSD from Java

This is not your usual generate code. Instead of generating an XSD based on Java classes, I wanted to generate an XSD from my Java code. The below code requires Apache XML Beans.

import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.impl.xb.xsdschema.*;

import java.math.BigInteger;

public class Test {
     public static void main(String[] args) throws IOException, IllegalAccessException, InstantiationException, XmlException {

           // Start by parsing in a base schema
           SchemaDocument parse = SchemaDocument.Factory.parse(
                       "<xs:schema xmlns:xs="">\n" +

           // Add a new element to the schema
           TopLevelElement topLevelElement = parse


           LocalElement localElement = topLevelElement

           localElement.setMinOccurs(new BigInteger("1")); // make it a required element
           localElement.setMaxOccurs(new BigInteger("1")); // make it a 1...1 element

           // Write to string and print to screen
           String s = parse.xmlText();



Comments are Disabled