是否可以从带注释的JAXB类生成XSD?

时间:2021-09-23 17:16:30

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 annotations. Is there a tool for this?

我已经编写了许多使用JAXB进行序列化的类,我想知道是否有一种方法可以根据注释为每个对象生成一个XSD文件。有这个工具吗?

Something like generate-xsd com/my/package/model/Unit.java would be awesome. Does anything exist to do this?

像generate-xsd com / my / package / model / Unit.java这样的东西真棒。有什么事可做吗?

1 个解决方案

#1


67  

Yes, you can use the generateSchema method on JAXBContext:

是的,您可以在JAXBContext上使用generateSchema方法:

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:

您利用SchemaOutputResolver的实现来控制输出的位置:

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;
    }

}

#1


67  

Yes, you can use the generateSchema method on JAXBContext:

是的,您可以在JAXBContext上使用generateSchema方法:

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:

您利用SchemaOutputResolver的实现来控制输出的位置:

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;
    }

}