How to use JAXB APIs to generate classes from xsd?

后端 未结 2 1208
南笙
南笙 2021-01-23 22:14

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.

i

2条回答
  •  故里飘歌
    2021-01-23 23:07

    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)

    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("."));
    

    This produced the desired files for me. You need tools.jar on the classpath. Happy code generation!

提交回复
热议问题