简体   繁体   中英

Generating XML Schema from JAXB class files in Ant

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

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

    }   

}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM