Java HTML / XML How to - Use JAXBContext to generate Schema








Question

We would like to know how to use JAXBContext to generate Schema.

Answer

//from www .  ja  va2s  .co m
import java.io.IOException;

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

public class Main {

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

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

    });

  }

}