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

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

本文介绍了是否可以从带注释的JAXB类生成XSD?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

generate-xsd com / my / package / model / Unit.java 之类的东西 awesome 。有什么事可做吗?

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

推荐答案

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

Yes, you can use the generateSchema method on JAXBContext:

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

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

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

}

这篇关于是否可以从带注释的JAXB类生成XSD?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-27 15:40