问题描述
我需要在不使用xjc命令或ant的情况下从.xsd生成bean类。我在Apache Axis2中找到了实现,但我无法生成工件。
I need to generate bean classes from .xsd without using xjc command or ant. i have found the implementation in Apache Axis2 but i am unable to generate the artifacts.
我写了以下代码,但是我得到了NullPointerException:
i have written the following code but i get NullPointerException :
SchemaCompiler sc = XJC.createSchemaCompiler();
URL url = new URL("file://E:\\JAXB\\books.xsd");
sc.parseSchema(new InputSource(url.toExternalForm()));
S2JJAXBModel model = sc.bind();
JCodeModel cm = model.generateCode(null, null);
cm.build(new FileCodeWriter(new File("E:\\JAXBTest")));
任何人都可以帮助我/提供一些链接???
Can anyone help me / provide some links???
推荐答案
虽然我没有尝试过Axis2的XJC,但是我尝试了Sun的,我很确定你的架构的URL是错误的:你需要三个斜杠(因为权威 部分被遗漏,因为它是具有绝对路径的本地资源
While I haven't tried Axis2's XJC, I tried Sun's and I am pretty sure that your URL for the schema is wrong: You need three slashes (since the "authority" part is left out since it's a local resource with an absolute path)
或者,甚至更简单,构造一个文件并在其上调用toURI(),如下所示: / p>
Or, even simpler, construct a File and call toURI() on it, like this:
SchemaCompiler sc = XJC.createSchemaCompiler();
File file = new File("D:\\my-dir\\my-schema.xsd");
sc.setErrorListener(... );
sc.parseSchema(new InputSource(file.toURI().toString()));
S2JJAXBModel model = sc.bind();
JCodeModel cm = model.generateCode(null, null);
cm.build(new File("."));
这为我生成了所需的文件。你需要在类路径上使用tools.jar。快乐的代码生成!
This produced the desired files for me. You need tools.jar on the classpath. Happy code generation!
这篇关于如何使用JAXB API从xsd生成类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!