本文介绍了在 Ant 中从 JAXB 类文件生成 XML 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用shemagen ant Task从类文件而不是源文件生成 xsd 模式?

Is it possible to use the shemagen ant Task to generate an xsd schema from class files instead of from source?

推荐答案

你可以很容易地写一些东西,然后从 Ant 中调用它:

You could probably write something fairly easily, and then call it from Ant:

import java.io.File;
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 SchemaGenerator {

    public static void main(String[] args) throws Exception {
        String contextPath = args[0];
        String outputDir = args[1];
        JAXBContext jc = JAXBContext.newInstance(contextPath);
        jc.generateSchema(new MySchemaOutputResolver(schemaFileName));
    }

    private static class MySchemaOutputResolver extends SchemaOutputResolver {

        private String outputDir;

        public MySchemaOutputResolver(String outputDir) {
            this.outputDir = outputDir;
        }

        public Result createOutput(String namespaceURI, String suggestedFileName) throws IOException {
            File file = new File(outputDir + "/" + suggestedFileName);
            StreamResult result = new StreamResult(file);
            result.setSystemId(file.toURI().toURL().toString());
            return result;
        }

    }

}

在您的上下文路径中,您需要一个 jaxb.in​​dex 文件,其中包含要包含在 JAXBContext 中的类列表.或者您可以将类名传递给 SchemaGenerator 类并通过 ClassLoader 加载它们.

In your context path you would need a jaxb.index file with a list of classes to be included in your JAXBContext. Or you could pass the class names to the SchemaGenerator class and load them via a ClassLoader.

这篇关于在 Ant 中从 JAXB 类文件生成 XML 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 00:23