本文介绍了从蚂蚁JAXB类文件生成XML模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能使用来生成的类文件,而不是从源XSD架构?
Is it possible to use the shemagen ant Task to generate an xsd schema from class files instead of from source?
推荐答案
您也许可以写的东西很容易,然后从蚂蚁调用它:
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.index文件,将包含在你的JAXBContext类的列表。或者你可以通过类名的SchemaGenerator类,并通过一个类加载器加载它们。
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.
这篇关于从蚂蚁JAXB类文件生成XML模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!