开发者

How to use JAXB APIs to generate classes from xsd?

开发者 https://www.devze.com 2023-02-01 23:29 出处:网络
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 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 have written the following code but i get NullPointerException :

SchemaCompiler sc = XJC.create开发者_C百科SchemaCompiler();
 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???


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!


    SchemaCompiler sc = XJC.createSchemaCompiler();
    File file = null;
    file = new File("Item.xsd");
    sc.parseSchema(new InputSource(file.toURI().toString()));
    S2JJAXBModel model = sc.bind();

    JCodeModel cm = model.generateCode(null, null);
    cm.build(new File("."));

//I am having Item.xsd in local directory and this code has generated classes. If i remove this xsd locally will get null pointer exception. Also note i am having my jaxb related jars in Java Build path of eclipse project.

0

精彩评论

暂无评论...
验证码 换一张
取 消