本文介绍了是否可以从带有 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 这样的东西会很棒.有什么东西可以做到这一点吗?

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?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 17:01