Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/ant/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Ant中的JAXB类文件生成XML模式_Java_Ant_Xsd_Jaxb_Schemagen - Fatal编程技术网

Java 从Ant中的JAXB类文件生成XML模式

Java 从Ant中的JAXB类文件生成XML模式,java,ant,xsd,jaxb,schemagen,Java,Ant,Xsd,Jaxb,Schemagen,是否可以使用从类文件而不是从源文件生成xsd架构?您可以相当轻松地编写一些内容,然后从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.S

是否可以使用从类文件而不是从源文件生成xsd架构?

您可以相当轻松地编写一些内容,然后从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.index文件,其中包含要包含在JAXBContext中的类列表。或者您可以将类名传递给SchemaGenerator类,并通过类加载器加载它们