Is it possible to generate a XSD from a JAXB-annotated class?

前端 未结 1 1707
鱼传尺愫
鱼传尺愫 2020-11-29 02:09

I\'ve written a number of classes using JAXB for serialization and I was wondering if there was a way to generate a XSD file for each of these objects based on the annotatio

1条回答
  •  渐次进展
    2020-11-29 02:30

    Yes, you can use the generateSchema method on JAXBContext:

    JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class);
    SchemaOutputResolver sor = new MySchemaOutputResolver();
    jaxbContext.generateSchema(sor);
    

    You leverage an implementation of SchemaOutputResolver to control where the output goes:

    public class MySchemaOutputResolver extends SchemaOutputResolver {
    
        public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
            File file = new File(suggestedFileName);
            StreamResult result = new StreamResult(file);
            result.setSystemId(file.toURI().toURL().toString());
            return result;
        }
    
    }
    

    0 讨论(0)
提交回复
热议问题